public async Task <IActionResult> AddRoleToUser(string userId) { IdentityUser user = new IdentityUser(); if (!string.IsNullOrEmpty(userId)) { user = await _userManager.FindByIdAsync(userId); } if (user == null) { return(RedirectToAction("IndexRoles", _roleManager.Roles)); } //reeds toegekende rollen var assignRolesToUserVM = new RolesForUser_VM() { AssignedRoles = await _userManager.GetRolesAsync(user), UnAssignedRoles = new List <string>(), User = user, UserId = userId }; //nog niet toegekende rollen foreach (var identityRole in _roleManager.Roles) { if (!await _userManager.IsInRoleAsync(user, identityRole.Name)) { assignRolesToUserVM.UnAssignedRoles.Add(identityRole.Name); } } return(View(assignRolesToUserVM)); }
public async Task <IActionResult> AddRoleToUser(string userId, string roleId) { User user = new User(); // vaak ApplicationUser if (!String.IsNullOrEmpty(userId)) { user = await _userManager.FindByIdAsync(userId); } Role role = new Role(); if (!String.IsNullOrEmpty(roleId)) { role = await _roleManager.FindByIdAsync(roleId); } if (role == null && user == null) { return(RedirectToAction("IndexRoles", _roleManager.Roles)); } //Reeds toegekende rollen var assignRolesToUserVM = new RolesForUser_VM() { AssignedRoles = await _userManager.GetRolesAsync(user), UnAssignedRoles = new List <string>(), User = user, UserId = userId }; //Nog niet toegekende rollen foreach (var identityRole in _roleManager.Roles) { if (!await _userManager.IsInRoleAsync(user, identityRole.Name)) { assignRolesToUserVM.UnAssignedRoles.Add(identityRole.Name); } } //Toon users in deze rol //foreach (var identityUser in _userManager.Users) //{ // if (!await _userManager.IsInRoleAsync(identityUser, role.Name)) // { // addUserToRoleViewModel.Users.Add(user); // } //} return(View(assignRolesToUserVM)); }
public async Task <IActionResult> AddRoleToUser(RolesForUser_VM rolesForUserVM) { var user = await _userManager.FindByIdAsync(rolesForUserVM.UserId); var role = await _roleManager.FindByNameAsync(rolesForUserVM.RoleId); var result = await _userManager.AddToRoleAsync(user, role.Name); if (result.Succeeded) { return(RedirectToAction("IndexUsers", _roleManager.Roles)); } else { Debug.WriteLine(result.Errors); } return(View(rolesForUserVM)); }
public async Task <IActionResult> AddUserToRole(RolesForUser_VM rolesForUserVM) { var user = await _userManager.FindByIdAsync(rolesForUserVM.UserId); var role = await _roleManager.FindByNameAsync(rolesForUserVM.RoleId); var result = await _userManager.AddToRoleAsync(user, role.NormalizedName); if (result.Succeeded) { return(RedirectToAction("IndexRoles", _roleManager.Roles)); } foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View(rolesForUserVM)); }