ResetPassword.cshtml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 ResetPasswordModel : PageModel
  13. {
  14. private readonly UserManager<IdentityUser> _userManager;
  15. public ResetPasswordModel(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. [Required]
  27. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  28. [DataType(DataType.Password)]
  29. public string Password { get; set; }
  30. [DataType(DataType.Password)]
  31. [Display(Name = "Confirm password")]
  32. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  33. public string ConfirmPassword { get; set; }
  34. public string Code { get; set; }
  35. }
  36. public IActionResult OnGet(string code = null)
  37. {
  38. if (code == null)
  39. {
  40. return BadRequest("A code must be supplied for password reset.");
  41. }
  42. else
  43. {
  44. Input = new InputModel
  45. {
  46. Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code))
  47. };
  48. return Page();
  49. }
  50. }
  51. public async Task<IActionResult> OnPostAsync()
  52. {
  53. if (!ModelState.IsValid)
  54. {
  55. return Page();
  56. }
  57. var user = await _userManager.FindByEmailAsync(Input.Email);
  58. if (user == null)
  59. {
  60. // Don't reveal that the user does not exist
  61. return RedirectToPage("./ResetPasswordConfirmation");
  62. }
  63. var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
  64. if (result.Succeeded)
  65. {
  66. return RedirectToPage("./ResetPasswordConfirmation");
  67. }
  68. foreach (var error in result.Errors)
  69. {
  70. ModelState.AddModelError(string.Empty, error.Description);
  71. }
  72. return Page();
  73. }
  74. }
  75. }