/// <summary> /// Sends an e-mail with a notification that the user password has changed. /// </summary> /// <param name="viewModel">Represents the view model of the e-mail.</param> public async Task SendPasswordChangedEmailAsync(EmailPasswordChangedViewModel 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 - Your password has been changed"; var htmlContent = await _renderer.RenderPartialToStringAsync("_EmailPasswordChangedPartial", 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() { // We get the current user. var user = await _userManager.GetUserAsync(User); // We check if the user exists or not. if (user == null) { // Display a message. TempData["StatusMessage"] = "Error: An error occured while trying to load the user data. If you are already logged in, please log out and try again."; // Redirect to the home page. return(RedirectToPage("/Index")); } // Define the view. View = new ViewModel { HasPassword = await _userManager.HasPasswordAsync(user) }; // 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()); } // Try to change the password or set a new one. var result = View.HasPassword ? await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword) : await _userManager.AddPasswordAsync(user, Input.NewPassword); // Check if the password update wasn't successful. if (!result.Succeeded) { // Go over the encountered errors foreach (var error in result.Errors) { // and add them to the model ModelState.AddModelError(string.Empty, error.Description); } // Return the page. return(Page()); } // Define a new view model for the e-mail. var emailViewModel = new EmailPasswordChangedViewModel { Email = user.Email, Url = _linkGenerator.GetUriByPage(HttpContext, "/Account/Index", handler: null, values: null), ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the e-mail changed e-mail to the user. await _emailSender.SendPasswordChangedEmailAsync(emailViewModel); // Re-sign in the user to update the changes. await _signInManager.RefreshSignInAsync(user); // Display a message. TempData["StatusMessage"] = "Success: The password was successfully updated."; // Redirect to page. return(RedirectToPage()); }