ForgotPassword.cshtml.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.ComponentModel.DataAnnotations;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Mvc.RazorPages;
  8. using Microsoft.AspNetCore.WebUtilities;
  9. namespace BO.AppServer.Web.Areas.Identity.Pages.Account
  10. {
  11. [AllowAnonymous]
  12. public class ForgotPasswordModel : PageModel
  13. {
  14. private readonly UserManager<IdentityUser> _userManager;
  15. public ForgotPasswordModel(UserManager<IdentityUser> userManager)
  16. {
  17. _userManager = userManager;
  18. }
  19. [BindProperty]
  20. public InputModel Input { get; set; }
  21. public class InputModel
  22. {
  23. [Required]
  24. [EmailAddress]
  25. public string Email { get; set; }
  26. }
  27. public async Task<IActionResult> OnPostAsync()
  28. {
  29. if (ModelState.IsValid)
  30. {
  31. var user = await _userManager.FindByEmailAsync(Input.Email);
  32. if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
  33. {
  34. // Don't reveal that the user does not exist or is not confirmed
  35. return RedirectToPage("./ForgotPasswordConfirmation");
  36. }
  37. // For more information on how to enable account confirmation and password reset please
  38. // visit https://go.microsoft.com/fwlink/?LinkID=532713
  39. var code = await _userManager.GeneratePasswordResetTokenAsync(user);
  40. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  41. var callbackUrl = Url.Page(
  42. "/Account/ResetPassword",
  43. pageHandler: null,
  44. values: new { area = "Identity", code },
  45. protocol: Request.Scheme);
  46. // TODO: enable IEmailSender
  47. /*
  48. await _emailSender.SendEmailAsync(
  49. Input.Email,
  50. "Reset Password",
  51. $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
  52. */
  53. return RedirectToPage("./ForgotPasswordConfirmation");
  54. }
  55. return Page();
  56. }
  57. }
  58. }