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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  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 MVCTemplate.Areas.Identity.Pages.Account
  14. {
  15. [AllowAnonymous]
  16. public class ResetPasswordModel : PageModel
  17. {
  18. private readonly UserManager<IdentityUser> _userManager;
  19. public ResetPasswordModel(UserManager<IdentityUser> userManager)
  20. {
  21. _userManager = userManager;
  22. }
  23. [BindProperty]
  24. public InputModel Input { get; set; }
  25. public class InputModel
  26. {
  27. [Required]
  28. [EmailAddress]
  29. public string Email { get; set; }
  30. [Required]
  31. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  32. [DataType(DataType.Password)]
  33. public string Password { get; set; }
  34. [DataType(DataType.Password)]
  35. [Display(Name = "Confirm password")]
  36. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  37. public string ConfirmPassword { get; set; }
  38. public string Code { get; set; }
  39. }
  40. public IActionResult OnGet(string code = null)
  41. {
  42. if (code == null)
  43. {
  44. return BadRequest("A code must be supplied for password reset.");
  45. }
  46. else
  47. {
  48. Input = new InputModel
  49. {
  50. Code = code
  51. };
  52. return Page();
  53. }
  54. }
  55. public async Task<IActionResult> OnPostAsync()
  56. {
  57. if (!ModelState.IsValid)
  58. {
  59. return Page();
  60. }
  61. var user = await _userManager.FindByEmailAsync(Input.Email);
  62. if (user == null)
  63. {
  64. // Don't reveal that the user does not exist
  65. return RedirectToPage("./ResetPasswordConfirmation");
  66. }
  67. var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
  68. if (result.Succeeded)
  69. {
  70. return RedirectToPage("./ResetPasswordConfirmation");
  71. }
  72. foreach (var error in result.Errors)
  73. {
  74. ModelState.AddModelError(string.Empty, error.Description);
  75. }
  76. return Page();
  77. }
  78. }
  79. }