public async Task ResetPassword(ResetPasswordRequest request) { request.ValidateAndThrow(); var user = await _userManager.FindByEmailAsync(request.Email); if (user != null) { // Check if the new password is valid var validator = new PasswordValidator <User>(); var result = await validator.ValidateAsync(_userManager, user, request.NewPassword); if (!result.Succeeded) { var errors = IdentityHelper.ErrorsToString(result); throw new AuthenticationException(ExceptionCodes.IDENTITY_ERROR_RESET_PASSWORD, errors, null, StatusCodes.Status400BadRequest); } result = await _userManager.ResetPasswordAsync(user, request.ResetPasswordToken, request.NewPassword); if (!result.Succeeded) { var errors = IdentityHelper.ErrorsToString(result); throw new AuthenticationException(ExceptionCodes.IDENTITY_ERROR_RESET_PASSWORD, errors, null, StatusCodes.Status400BadRequest); } } else { throw new AuthenticationException(ExceptionCodes.IDENTITY_ERROR_RESET_PASSWORD, "Invalid request", null, StatusCodes.Status400BadRequest); } }
public async Task <CreateUserResponse> Create(CreateUserRequest request) { request.ValidateAndThrow(); var result = await _userManager.CreateAsync(request.User); if (!result.Succeeded) { throw new AuthenticationException(ExceptionCodes.IDENTITY_ERROR_CREATING_USER, IdentityHelper.ErrorsToString(result), null, StatusCodes.Status400BadRequest); } var userCreated = await _userManager.FindByNameAsync(request.User.UserName); return(new CreateUserResponse { User = userCreated }); }