//**********************************************************************// public async Task<IActionResult> UsersInRole(string Id) { var role = await roleManager.FindByIdAsync(Id); var model = new RoleAndUsers(); //include users in this role if (role == null) { ModelState.AddModelError("", $"Role does not exist"); } else { ViewBag.roleName = role.Name; ViewBag.roleId = role.Id; var usersNotInRole = new List<IdentityUser>(); foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.UsersInThisRole.Add(new IdentityUser { UserName = user.UserName, Email = user.Email }); } else { usersNotInRole.Add(new IdentityUser { UserName = user.UserName, Email = user.Email }); } ViewBag.UsersNotInRole = new SelectList(usersNotInRole.OrderBy(a => a.UserName).ToList()); } model.UsersInThisRole = model.UsersInThisRole.OrderBy(a => a.UserName).ToList(); } return View(model); }
public async Task<IActionResult> RemoveUser(RoleAndUsers model) { var role = await roleManager.FindByIdAsync(model.RoleId); var user = await userManager.FindByNameAsync(model.UserName); IdentityResult result = await userManager.RemoveFromRoleAsync(user, role.Name); if (result.Succeeded) { TempData["message"] = $"User: {user.UserName} removed from: '{role.Name}' "; } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return RedirectToAction("UsersInRole", new { id = model.RoleId }); }