public async Task <IActionResult> SetUserStatusRole([FromBody] UserStatusRole userStatusRole) { var selectedUser = await _userManager.Users.SingleOrDefaultAsync(u => u.UserName == userStatusRole.UserName); if (selectedUser == null) { return(BadRequest($"User {userStatusRole.UserName} not found in database.")); } var currentRoles = await _userManager.GetRolesAsync(selectedUser); foreach (var currentRole in currentRoles) { var removeResult = await _userManager.RemoveFromRoleAsync(selectedUser, currentRole); if (!removeResult.Succeeded) { var appEx = new WapException("Unable to remove role."); foreach (IdentityError error in removeResult.Errors) { appEx.Details.Add(error.Description); } throw appEx; } } if (userStatusRole.StatusRole == "Inactive") { selectedUser.IsActive = false; await _userManager.UpdateAsync(selectedUser); return(Ok(userStatusRole.StatusRole)); } selectedUser.IsActive = true; var roleResult = await _userManager.AddToRoleAsync(selectedUser, userStatusRole.StatusRole); if (!roleResult.Succeeded) { var appEx = new WapException("Unable to set user's role to Agent."); foreach (var error in roleResult.Errors) { appEx.Details.Add(error.Description); } throw appEx; } return(Ok(userStatusRole.StatusRole)); }
public async Task <IActionResult> Register(UserRegistration userRegistration) { WapUser newUser = Mapper.Map <WapUser>(userRegistration); try { var createResult = await _userManager.CreateAsync(newUser, userRegistration.Password); if (!createResult.Succeeded) { var appEx = new WapException("Unable to register user."); createResult.Errors.ToList().ForEach(error => appEx.Details.Add(error.Description)); throw appEx; } return(Ok()); } catch (WapException e) { return(BadRequest(e.AsViewModel())); } }