You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResetPassword.cshtml.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.ComponentModel.DataAnnotations;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Identity;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Mvc.RazorPages;
  6. namespace SecureSharing.Areas.Identity.Pages.Account;
  7. [AllowAnonymous]
  8. public sealed class ResetPasswordModel : PageModel
  9. {
  10. private readonly UserManager<IdentityUser> _userManager;
  11. public ResetPasswordModel(UserManager<IdentityUser> userManager)
  12. {
  13. _userManager = userManager;
  14. }
  15. [BindProperty] public InputModel Input { get; set; }
  16. public IActionResult OnGet(string code = null)
  17. {
  18. if (code is null) return BadRequest("A code must be supplied for password reset.");
  19. Input = new InputModel
  20. {
  21. Code = code
  22. };
  23. return Page();
  24. }
  25. public async Task<IActionResult> OnPostAsync()
  26. {
  27. if (!ModelState.IsValid) return Page();
  28. var user = await _userManager.FindByEmailAsync(Input.Email);
  29. if (user is null)
  30. // Don't reveal that the user does not exist
  31. return RedirectToPage("./ResetPasswordConfirmation");
  32. var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
  33. if (result.Succeeded) return RedirectToPage("./ResetPasswordConfirmation");
  34. foreach (var error in result.Errors) ModelState.AddModelError(string.Empty, error.Description);
  35. return Page();
  36. }
  37. public sealed class InputModel
  38. {
  39. [Required] [EmailAddress] public string Email { get; set; }
  40. [Required]
  41. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
  42. MinimumLength = 6)]
  43. [DataType(DataType.Password)]
  44. public string Password { get; set; }
  45. [DataType(DataType.Password)]
  46. [Display(Name = "Confirm password")]
  47. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  48. public string ConfirmPassword { get; set; }
  49. public string Code { get; set; }
  50. }
  51. }