public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { BejebejeUser user = await _userManager.FindByEmailAsync(model.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { // Don't reveal that the user does not exist or is not confirmed return(RedirectToPage("./ForgotPasswordConfirmation")); } // For more information on how to enable account confirmation and password reset please // visit https://go.microsoft.com/fwlink/?LinkID=532713 string code = await _userManager.GeneratePasswordResetTokenAsync(user); string callbackUrl = Url.Action( "ResetPassword", "Account", new { code }, Request.Scheme); EmailForgotPasswordViewModel viewModel = new EmailForgotPasswordViewModel(); viewModel.UserDisplayUsername = user.DisplayUsername; viewModel.Code = callbackUrl; viewModel.UserEmailAddress = user.Email; await _emailService.SendForgotPasswordEmailAsync(viewModel); return(RedirectToAction("ForgotPasswordConfirmation")); } return(RedirectToAction("Index", "Home")); }
public async Task SendForgotPasswordEmailAsync(EmailForgotPasswordViewModel emailViewModel) { string emailTemplateFolderPath = Path.Combine(_environment.ContentRootPath, "EmailTemplates"); string reportEmailTemplatePath = Path.Combine(emailTemplateFolderPath, "ForgotPassword.cshtml"); RazorLightEngine engine = new RazorLightEngineBuilder() .UseFilesystemProject(emailTemplateFolderPath) .UseMemoryCachingProvider() .Build(); string emailHtmlBody = await engine.CompileRenderAsync(reportEmailTemplatePath, emailViewModel); MailMessage mailMessage = new MailMessage( emailViewModel.UserEmailAddress, _emailConfiguration.OutgoingEmailAddress, "Forgotten Password", emailHtmlBody); mailMessage.IsBodyHtml = true; string operationIdentity = "Forgotten password email"; SendEmailAsync(mailMessage, operationIdentity); }