public static string GetFormatedForgotPasswordEmailTemplate(ForgotEmailData forgotEmailData, string path) { string text = File.ReadAllText(path); text = text.Replace("{siteName}", forgotEmailData.SiteName); text = text.Replace("{siteUrl}", forgotEmailData.SiteUrl); text = text.Replace("{email}", forgotEmailData.Email); text = text.Replace("{company}", forgotEmailData.Company); text = text.Replace("{resetUrl}", forgotEmailData.PasswordResetUrl); return(text); }
public async Task <IActionResult> SendPasswordResetLink([FromBody] ForgotPasswordReq model) { try { var user = await _userManager.FindByEmailAsync(model.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { return(NotFound("Email address is not registered or wrong email.")); } var code = await _userManager.GeneratePasswordResetTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); //insert to database PasswordResetToken passwordResetToken = new PasswordResetToken { UserID = user.Id, IsActive = true, RegistedDate = DateTime.Now, Token = code }; _context.passwordResetTokens.Add(passwordResetToken); _ = _context.SaveChangesAsync(); ForgotEmailData forgotEmailData = new ForgotEmailData { Company = _config.Value.CompanyName, Email = model.Email, PasswordResetUrl = _config.Value.ResetEmailUrl + "?token=" + code, SiteName = _config.Value.SolutionName, SiteUrl = _config.Value.BaseURL }; await _emailSender.SendEmailAsync(model.Email, "APlus Account Password Reset", DataFormatManager.GetFormatedForgotPasswordEmailTemplate(forgotEmailData, _hostingEnvironment.ContentRootPath + _templateParams.Value.ForgotPasswordMailTemplate)); return(Ok("Email sent successfully")); } catch (Exception ex) { throw ex; } }