public async Task <IActionResult> ResetPassword(ResetPasswordParameters parameters) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(state => state.Errors) .Select(error => error.ErrorMessage) .FirstOrDefault())); } var user = await _userManager.FindByIdAsync(parameters.UserId); if (user == null) { _logger.LogInformation("User does not exist: {0}", parameters.UserId); return(BadRequest("User does not exist")); } #region Reset Password Successful Email try { IdentityResult result = await _userManager.ResetPasswordAsync(user, parameters.Token, parameters.Password); if (result.Succeeded) { #region Email Successful Password change var email = new EmailMessage(); email.ToAddresses.Add(new EmailAddress(user.Email, user.Email)); email.FromAddresses.Add(new EmailAddress("*****@*****.**", "*****@*****.**")); email = EmailTemplates.BuildPasswordResetEmail(email, user.UserName); //Replace First UserName with Name if you want to add name to Registration Form _logger.LogInformation("Reset Password Successful Email Sent: {0}", user.Email); await _emailService.SendEmailAsync(email); #endregion return(Ok(new { success = "true" })); } else { _logger.LogInformation("Error while resetting the password!: {0}", user.UserName); return(BadRequest(string.Format("Error while resetting the password!: {0}", user.UserName))); } } catch (Exception ex) { _logger.LogInformation("Reset Password failed: {0}", ex.Message); return(BadRequest(string.Format("Error while resetting the password!: {0}", ex.Message))); } #endregion }
public async Task <ApiResponse> ResetPassword(ResetPasswordDto parameters) { if (!ModelState.IsValid) { return(new ApiResponse(400, "User Model is Invalid")); } var user = await _userManager.FindByIdAsync(parameters.UserId); if (user == null) { _logger.LogInformation("User does not exist: {0}", parameters.UserId); return(new ApiResponse(404, "User does not exist")); } #region Reset Password Successful Email try { IdentityResult result = await _userManager.ResetPasswordAsync(user, parameters.Token, parameters.Password); if (result.Succeeded) { #region Email Successful Password change var email = new EmailMessageDto(); email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email)); email = EmailTemplates.BuildPasswordResetEmail(email, user.UserName); //Replace First UserName with Name if you want to add name to Registration Form _logger.LogInformation("Reset Password Successful Email Sent: {0}", user.Email); await _emailService.SendEmailAsync(email); #endregion return(new ApiResponse(200, String.Format("Reset Password Successful Email Sent: {0}", user.Email))); } else { _logger.LogInformation("Error while resetting the password!: {0}", user.UserName); return(new ApiResponse(400, string.Format("Error while resetting the password!: {0}", user.UserName))); } } catch (Exception ex) { _logger.LogInformation("Reset Password failed: {0}", ex.Message); return(new ApiResponse(400, string.Format("Error while resetting the password!: {0}", ex.Message))); } #endregion }