ForgotPassword.cshtml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. #nullable disable
  4. using System;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Text;
  7. using System.Text.Encodings.Web;
  8. using System.Threading.Tasks;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Identity;
  11. using Microsoft.AspNetCore.Identity.UI.Services;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.AspNetCore.Mvc.RazorPages;
  14. using Microsoft.AspNetCore.WebUtilities;
  15. namespace bitforum.Areas.Identity.Pages.Account
  16. {
  17. public class ForgotPasswordModel : PageModel
  18. {
  19. private readonly UserManager<IdentityUser> _userManager;
  20. private readonly IEmailSender _emailSender;
  21. public ForgotPasswordModel(UserManager<IdentityUser> userManager, IEmailSender emailSender)
  22. {
  23. _userManager = userManager;
  24. _emailSender = emailSender;
  25. }
  26. /// <summary>
  27. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  28. /// directly from your code. This API may change or be removed in future releases.
  29. /// </summary>
  30. [BindProperty]
  31. public InputModel Input { get; set; }
  32. /// <summary>
  33. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  34. /// directly from your code. This API may change or be removed in future releases.
  35. /// </summary>
  36. public class InputModel
  37. {
  38. /// <summary>
  39. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  40. /// directly from your code. This API may change or be removed in future releases.
  41. /// </summary>
  42. [Required]
  43. [EmailAddress]
  44. public string Email { get; set; }
  45. }
  46. public async Task<IActionResult> OnPostAsync()
  47. {
  48. if (ModelState.IsValid)
  49. {
  50. var user = await _userManager.FindByEmailAsync(Input.Email);
  51. if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
  52. {
  53. // Don't reveal that the user does not exist or is not confirmed
  54. return RedirectToPage("./ForgotPasswordConfirmation");
  55. }
  56. // For more information on how to enable account confirmation and password reset please
  57. // visit https://go.microsoft.com/fwlink/?LinkID=532713
  58. var code = await _userManager.GeneratePasswordResetTokenAsync(user);
  59. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  60. var callbackUrl = Url.Page(
  61. "/Account/ResetPassword",
  62. pageHandler: null,
  63. values: new { area = "Identity", code },
  64. protocol: Request.Scheme);
  65. await _emailSender.SendEmailAsync(
  66. Input.Email,
  67. "Reset Password",
  68. $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
  69. return RedirectToPage("./ForgotPasswordConfirmation");
  70. }
  71. return Page();
  72. }
  73. }
  74. }