public async Task <IActionResult> Edit(string userId, List <string> roles) { var account = _userManagerService.FindByIdRole(userId); if (account != null) { // получем список ролей пользователя var userRoles = await _userManagerService.GetRolesByAccount(userId); // получаем все роли var allRoles = _userManagerService.ShowListOfRoles(); // получаем список ролей, которые были добавлены var addedRoles = roles.Except(userRoles); // получаем роли, которые были удалены var removedRoles = userRoles.Except(roles); await _userManagerService.AddToRole(userId, addedRoles); await _userManagerService.RemovedFromRoles(userId, removedRoles); return(RedirectToAction("UserList")); } return(NotFound()); }
public ActionResult SetToRole(string name) { if (name == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var targetUserId = GetUserId(name); if (targetUserId == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } var isTargetUserModerator = _userManagerService.IsInRole(targetUserId, "Moderator"); if (isTargetUserModerator) { _userManagerService.RemoveFromRole(targetUserId, "Moderator"); } else { _userManagerService.AddToRole(targetUserId, "Moderator"); } return(UserManagement()); }