public async Task <IActionResult> EditRole(string roleid, string userid)
        {
            if (ModelState.IsValid)
            {
                IdentityRole role = await _roleManager.FindByIdAsync(roleid);

                User user = await _userManager.FindByIdAsync(userid);

                IdentityResult result;
                if (await _userManager.IsInRoleAsync(user, role.Name))
                {
                    result = await _userManager.RemoveFromRoleAsync(user, role.Name);
                }
                else
                {
                    result = await _userManager.AddToRoleAsync(user, role.Name);
                }

                if (result.Succeeded)
                {
                    return(RedirectToAction(nameof(EditRole), new { id = role.Id }));
                }
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }
            }
            IdentityRole role1 = await _roleManager.FindByIdAsync(roleid);

            IEnumerable <User> members = await _userManager.GetUsersInRoleAsync(role1.Name);

            RoleEditViewModel model = new RoleEditViewModel()
            {
                Members    = members,
                NonMembers = _userManager.Users.Except(members),
                Role       = role1
            };

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> EditUser(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await _userManager.FindByIdAsync(model.Id);

                IEnumerable <string> roles = _roleManager.GetAllRoles();
                foreach (string role in roles)
                {
                    await _userManager.RemoveFromRoleAsync(user, role);
                }

                if (model.Roles != null)
                {
                    foreach (KeyValuePair <string, bool> role in model.Roles)
                    {
                        await _userManager.AddToRoleAsync(user, role.Key);
                    }
                }

                user.FirstName = model.FirstName;
                user.LastName  = model.LastName;
                user.UserName  = model.UserName;
                user.Email     = model.Email;
                //user.Status = model.Status;
                user.NormalizedEmail    = user.Email.ToUpper();
                user.NormalizedUserName = user.UserName.ToUpper();


                IdentityResult result = await _userManager.UpdateUserAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction(nameof(UserIndex)));
                }
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            User tempUser = await _userManager.FindByIdAsync(model.Id);

            IEnumerable <string> allRoles  = _roleManager.GetAllRoles();
            IList <string>       userRoles = await _userManager.GetRolesAsync(tempUser);

            Dictionary <string, bool> dictionary = new Dictionary <string, bool>();

            foreach (string role in allRoles)
            {
                if (userRoles.Contains(role))
                {
                    dictionary.Add(role, true);
                }
                else
                {
                    dictionary.Add(role, false);
                }
            }
            model.Roles = dictionary;
            return(View(model));
        }