/// <summary> /// Sends an e-mail with instructions on resetting the user password. /// </summary> /// <param name="viewModel">Represents the view model of the e-mail.</param> public async Task SendPasswordResetEmailAsync(EmailPasswordResetViewModel viewModel) { // Define the variables for the e-mail. var apiKey = _configuration.GetSection("Authentication:SendGrid:AppKey").Value; var client = new SendGridClient(apiKey); var from = new EmailAddress(_configuration.GetSection("EmailSender:Email").Value, _configuration.GetSection("EmailSender:Name").Value); var to = new EmailAddress(viewModel.Email, viewModel.Email); var subject = "NetControl4BioMed - Reset your password"; var htmlContent = await _renderer.RenderPartialToStringAsync("_EmailPasswordResetPartial", viewModel); var msg = MailHelper.CreateSingleEmail(from, to, subject, string.Empty, htmlContent); // Send the e-mail containing the URL. await client.SendEmailAsync(msg); }
public async Task <IActionResult> OnPostAsync() { // Check if the reCaptcha is valid. if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The reCaptcha verification failed."); // Return the page. return(Page()); } // Check if the provided model is not valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields."); // Return the page. return(Page()); } // Get the user with the provided e-mail. var user = await _userManager.FindByEmailAsync(Input.Email); // Check if there wasn't any user found. if (user == null) { // Display a message. TempData["StatusMessage"] = "Error: The provided e-mail address is not associated with any user."; // Redirect to the page. return(RedirectToPage("/Account/Index")); } // Generate the password reset code for the user. var code = await _userManager.GeneratePasswordResetTokenAsync(user); // Get the callback URL to be encoded in the e-mail. var callbackUrl = _linkGenerator.GetUriByPage(HttpContext, "/Identity/ResetPassword", handler: null, values: new { code = code }); var encodedUrl = HtmlEncoder.Default.Encode(callbackUrl); // Define a new view model for the e-mail. var emailViewModel = new EmailPasswordResetViewModel { Email = user.Email, Url = encodedUrl, ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the password reset e-mail for the user. await _emailSender.SendPasswordResetEmailAsync(emailViewModel); // Display a message. TempData["StatusMessage"] = "Success: Please check your e-mail. A message containing instructions for resetting your password has been sent."; // Redirect to the home page. return(RedirectToPage("/Index")); }