public IdentityResult RemoveFromRoles(ApplicationUser user, string[] roleNames)
        {
            var uid = Convert.ToInt32(user.Id);

            foreach (var rName in roleNames)
            {
                var role = _context.AppRoles.Where(r => r.RoleName == rName).FirstOrDefault();
                if (role != null)
                {
                    UsersInRolesModel usersInRoles = _context.UsersInRoles.Find(uid, role.RoleId);
                    _context.UsersInRoles.Remove(usersInRoles);
                }
            }
            try
            {
                _context.SaveChanges();
                return(IdentityResult.Success);
            }
            catch (Exception e)
            {
                IdentityError[] errors = new IdentityError[1];
                errors[0].Code        = "500";
                errors[0].Description = e.Message;
                return(IdentityResult.Failed());
            }
        }
        public IdentityResult AddToRole(ApplicationUser user, string roleName)
        {
            var uid  = Convert.ToInt32(user.Id);
            var role = _context.AppRoles.Where(r => r.RoleName == roleName).FirstOrDefault();

            if (role != null)
            {
                UsersInRolesModel usersInRoles = new UsersInRolesModel();
                usersInRoles.UserId   = uid;
                usersInRoles.UserName = user.UserName;
                usersInRoles.RoleId   = role.RoleId;
                try
                {
                    _context.UsersInRoles.Add(usersInRoles);
                    _context.SaveChanges();
                    return(IdentityResult.Success);
                }
                catch (Exception e)
                {
                    IdentityError[] errors = new IdentityError[1];
                    errors[0].Code        = "500";
                    errors[0].Description = e.Message;
                    return(IdentityResult.Failed());
                }
            }
            else
            {
                IdentityError[] errors = new IdentityError[1];
                errors[0].Code        = "500";
                errors[0].Description = "無此腳色";
                return(IdentityResult.Failed());
            }
        }
Esempio n. 3
0
        public void AddUserToRole(string username, string roleName)
        {
            var user = _context.AppUsers.Where(u => u.UserName == username).FirstOrDefault();

            if (user != null)
            {
                var role = _context.AppRoles.Where(r => r.RoleName == roleName).FirstOrDefault();
                if (role != null)
                {
                    UsersInRolesModel usersInRolesModel = new UsersInRolesModel();
                    usersInRolesModel.UserId   = user.Id;
                    usersInRolesModel.UserName = user.UserName;
                    usersInRolesModel.RoleId   = role.RoleId;
                    _context.UsersInRoles.Add(usersInRolesModel);
                    _context.SaveChanges();
                }
            }
        }