public async Task <IActionResult> ForgotPassword(ForgotPasswordParameters parameters) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(state => state.Errors) .Select(error => error.ErrorMessage) .FirstOrDefault())); } var user = await _userManager.FindByEmailAsync(parameters.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { _logger.LogInformation("Forgot Password with non-existent email / user: {0}", parameters.Email); // Don't reveal that the user does not exist or is not confirmed return(Ok(new { success = "true" })); } #region Forgot Password Email try { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 var token = await _userManager.GeneratePasswordResetTokenAsync(user); string callbackUrl = string.Format("{0}/Account/ResetPassword/{1}?token={2}", _configuration["ApplicationUrl"], user.Id, token); //token must be a query string parameter as it is very long var email = new EmailMessage(); email.ToAddresses.Add(new EmailAddress(user.Email, user.Email)); email.FromAddresses.Add(new EmailAddress("*****@*****.**", "*****@*****.**")); email = EmailTemplates.BuildForgotPasswordEmail(email, user.UserName, callbackUrl, token); //Replace First UserName with Name if you want to add name to Registration Form _logger.LogInformation("Forgot Password Email Sent: {0}", user.Email); await _emailService.SendEmailAsync(email); } catch (Exception ex) { _logger.LogInformation("Forgot Password email failed: {0}", ex.Message); } #endregion return(Ok(new { success = "true" })); }
public async Task <ApiResponse> ForgotPassword(ForgotPasswordDto parameters) { if (!ModelState.IsValid) { return(new ApiResponse(400, "User Model is Invalid")); } var user = await _userManager.FindByEmailAsync(parameters.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { _logger.LogInformation("Forgot Password with non-existent email / user: {0}", parameters.Email); // Don't reveal that the user does not exist or is not confirmed return(new ApiResponse(200, "Success")); } #region Forgot Password Email try { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 var token = await _userManager.GeneratePasswordResetTokenAsync(user); string callbackUrl = string.Format("{0}/Account/ResetPassword/{1}?token={2}", _configuration["BlazorBoilerplate:ApplicationUrl"], user.Id, token); //token must be a query string parameter as it is very long var email = new EmailMessageDto(); email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email)); email = EmailTemplates.BuildForgotPasswordEmail(email, user.UserName, callbackUrl, token); //Replace First UserName with Name if you want to add name to Registration Form _logger.LogInformation("Forgot Password Email Sent: {0}", user.Email); await _emailService.SendEmailAsync(email); return(new ApiResponse(200, "Forgot Password Email Sent")); } catch (Exception ex) { _logger.LogInformation("Forgot Password email failed: {0}", ex.Message); } #endregion return(new ApiResponse(200, "Success")); }