ResetPassword.cshtml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Threading.Tasks;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.Identity;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Mvc.RazorPages;
  12. using Microsoft.AspNetCore.WebUtilities;
  13. namespace bitforum.Areas.Identity.Pages.Account
  14. {
  15. public class ResetPasswordModel : PageModel
  16. {
  17. private readonly UserManager<IdentityUser> _userManager;
  18. public ResetPasswordModel(UserManager<IdentityUser> userManager)
  19. {
  20. _userManager = userManager;
  21. }
  22. /// <summary>
  23. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  24. /// directly from your code. This API may change or be removed in future releases.
  25. /// </summary>
  26. [BindProperty]
  27. public InputModel Input { get; set; }
  28. /// <summary>
  29. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  30. /// directly from your code. This API may change or be removed in future releases.
  31. /// </summary>
  32. public class InputModel
  33. {
  34. /// <summary>
  35. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  36. /// directly from your code. This API may change or be removed in future releases.
  37. /// </summary>
  38. [Required]
  39. [EmailAddress]
  40. public string Email { get; set; }
  41. /// <summary>
  42. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  43. /// directly from your code. This API may change or be removed in future releases.
  44. /// </summary>
  45. [Required]
  46. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  47. [DataType(DataType.Password)]
  48. public string Password { get; set; }
  49. /// <summary>
  50. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  51. /// directly from your code. This API may change or be removed in future releases.
  52. /// </summary>
  53. [DataType(DataType.Password)]
  54. [Display(Name = "Confirm password")]
  55. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  56. public string ConfirmPassword { get; set; }
  57. /// <summary>
  58. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  59. /// directly from your code. This API may change or be removed in future releases.
  60. /// </summary>
  61. [Required]
  62. public string Code { get; set; }
  63. }
  64. public IActionResult OnGet(string code = null)
  65. {
  66. if (code == null)
  67. {
  68. return BadRequest("A code must be supplied for password reset.");
  69. }
  70. else
  71. {
  72. Input = new InputModel
  73. {
  74. Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code))
  75. };
  76. return Page();
  77. }
  78. }
  79. public async Task<IActionResult> OnPostAsync()
  80. {
  81. if (!ModelState.IsValid)
  82. {
  83. return Page();
  84. }
  85. var user = await _userManager.FindByEmailAsync(Input.Email);
  86. if (user == null)
  87. {
  88. // Don't reveal that the user does not exist
  89. return RedirectToPage("./ResetPasswordConfirmation");
  90. }
  91. var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
  92. if (result.Succeeded)
  93. {
  94. return RedirectToPage("./ResetPasswordConfirmation");
  95. }
  96. foreach (var error in result.Errors)
  97. {
  98. ModelState.AddModelError(string.Empty, error.Description);
  99. }
  100. return Page();
  101. }
  102. }
  103. }