ResetPassword.cshtml.cs 4.4 KB

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