Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ResetPassword.cshtml.cs 2.1KB

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