ForgotPassword.cshtml.cs 3.4 KB

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