public async Task <IHttpActionResult> ManageUsersInRole(UsersInRoleDto usersInRoleDto) { var role = await NdRoleManager.FindByIdAsync(usersInRoleDto.Id); if (role == null) { ModelState.AddModelError("", "Role does not exist"); return(BadRequest(ModelState)); } foreach (string user in usersInRoleDto.EnrolledUsers) { var ndUser = await NdUserManager.FindByIdAsync(user); if (ndUser == null) { ModelState.AddModelError("", string.Format("User: {0} does not exists", user)); continue; } if (!NdUserManager.IsInRole(user, role.Name)) { IdentityResult result = await NdUserManager.AddToRoleAsync(user, role.Name); if (!result.Succeeded) { ModelState.AddModelError("", string.Format("User: {0} could not be added to role", user)); } } } foreach (string user in usersInRoleDto.RemovedUsers) { var ndUser = await NdUserManager.FindByIdAsync(user); if (ndUser == null) { ModelState.AddModelError("", string.Format("User: {0} does not exists", user)); continue; } IdentityResult result = await NdUserManager.RemoveFromRoleAsync(user, role.Name); if (!result.Succeeded) { ModelState.AddModelError("", string.Format("User: {0} could not be removed from role", user)); } } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok()); }