public async Task <IActionResult> UpdateUserPassword(string userId, UserEditPasswordRequest passwordToEdit) { var response = await _userService.UpdateUserPassword(userId, passwordToEdit); if (response.Succeeded) { return(Ok()); } return(BadRequest(response.Errors)); }
public async Task <IdentityResult> UpdateUserPassword(string userId, UserEditPasswordRequest passwordToEdit) { var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(null); } var result = await _userManager.ChangePasswordAsync(user, passwordToEdit.CurrentPassword, passwordToEdit.NewPassword); return(result); }
public IActionResult EditPassword(string username, [FromForm] UserEditPasswordRequest editRequest) { var userId = AuthController.GetUserIdFromPrincipal(Request, config.Secret); var user = authUnit.Users.GetUserById(userId); // Validate user if (user == null) { return(NotFound()); } if (user.Username != username) { return(Unauthorized()); } // Compare existing password var oldHash = cryptoService.Compute(editRequest.OldPassword, user.PasswordSalt); if (!cryptoService.Compare(user.Password, oldHash)) { return(BadRequest()); } // Set new password var newHash = cryptoService.Compute(editRequest.NewPassword); user.Password = newHash; user.PasswordSalt = cryptoService.Salt; authUnit.Users.UpdateUser(user); authUnit.Complete(); return(NoContent()); }