public async Task <ActionResult> DeleteUserRole(string id, string rid) { try { await _context.DeleteUserRole(_userManager, id, rid); return(new NoContentResult()); } catch (ControllerNotFoundException e) { return(NotFound(e.Message)); } catch (ControllerBadRequestException e) { return(BadRequest(e.Message)); } catch (ControllerUnauthorizedException) { return(new UnauthorizedResult()); } }
public async Task <IActionResult> EditRoles(EditRolesModel roleadd) { try { CovenantUser user = await _context.GetUserByUsername(roleadd.UserName); IEnumerable <string> userRoles = (await _context.GetUserRoles(user.Id)).Select(UR => { Task <IdentityRole> t = _context.GetRole(UR.RoleId); t.Wait(); return(t.Result.Name); }); IEnumerable <string> userRolesRemain = userRoles.Where(UR => roleadd.Rolenames.Contains(UR)); foreach (string rolename in roleadd.Rolenames) { // Selected role that has not been added, must add if (!userRolesRemain.Contains(rolename)) { IdentityRole role = await _context.GetRoleByName(rolename); await _context.CreateUserRole(_userManager, user.Id, role.Id); } } IEnumerable <string> userRolesNotRemain = userRoles.Where(UR => !roleadd.Rolenames.Contains(UR)); foreach (string rolename in userRolesNotRemain) { // Did not select role that is already added, must remove IdentityRole role = await _context.GetRoleByName(rolename); await _context.DeleteUserRole(_userManager, user.Id, role.Id); } return(RedirectToAction(nameof(Index))); } catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException) { return(RedirectToAction(nameof(Index))); } }