ChangePassword.cshtml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Threading.Tasks;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.Extensions.Logging;
  11. namespace bitforum.Areas.Identity.Pages.Account.Manage
  12. {
  13. public class ChangePasswordModel : PageModel
  14. {
  15. private readonly UserManager<IdentityUser> _userManager;
  16. private readonly SignInManager<IdentityUser> _signInManager;
  17. private readonly ILogger<ChangePasswordModel> _logger;
  18. public ChangePasswordModel(
  19. UserManager<IdentityUser> userManager,
  20. SignInManager<IdentityUser> signInManager,
  21. ILogger<ChangePasswordModel> logger)
  22. {
  23. _userManager = userManager;
  24. _signInManager = signInManager;
  25. _logger = logger;
  26. }
  27. /// <summary>
  28. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  29. /// directly from your code. This API may change or be removed in future releases.
  30. /// </summary>
  31. [BindProperty]
  32. public InputModel Input { get; set; }
  33. /// <summary>
  34. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  35. /// directly from your code. This API may change or be removed in future releases.
  36. /// </summary>
  37. [TempData]
  38. public string StatusMessage { get; set; }
  39. /// <summary>
  40. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  41. /// directly from your code. This API may change or be removed in future releases.
  42. /// </summary>
  43. public class InputModel
  44. {
  45. /// <summary>
  46. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  47. /// directly from your code. This API may change or be removed in future releases.
  48. /// </summary>
  49. [Required]
  50. [DataType(DataType.Password)]
  51. [Display(Name = "Current password")]
  52. public string OldPassword { get; set; }
  53. /// <summary>
  54. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  55. /// directly from your code. This API may change or be removed in future releases.
  56. /// </summary>
  57. [Required]
  58. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  59. [DataType(DataType.Password)]
  60. [Display(Name = "New password")]
  61. public string NewPassword { get; set; }
  62. /// <summary>
  63. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  64. /// directly from your code. This API may change or be removed in future releases.
  65. /// </summary>
  66. [DataType(DataType.Password)]
  67. [Display(Name = "Confirm new password")]
  68. [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
  69. public string ConfirmPassword { get; set; }
  70. }
  71. public async Task<IActionResult> OnGetAsync()
  72. {
  73. var user = await _userManager.GetUserAsync(User);
  74. if (user == null)
  75. {
  76. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  77. }
  78. var hasPassword = await _userManager.HasPasswordAsync(user);
  79. if (!hasPassword)
  80. {
  81. return RedirectToPage("./SetPassword");
  82. }
  83. return Page();
  84. }
  85. public async Task<IActionResult> OnPostAsync()
  86. {
  87. if (!ModelState.IsValid)
  88. {
  89. return Page();
  90. }
  91. var user = await _userManager.GetUserAsync(User);
  92. if (user == null)
  93. {
  94. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  95. }
  96. var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword);
  97. if (!changePasswordResult.Succeeded)
  98. {
  99. foreach (var error in changePasswordResult.Errors)
  100. {
  101. ModelState.AddModelError(string.Empty, error.Description);
  102. }
  103. return Page();
  104. }
  105. await _signInManager.RefreshSignInAsync(user);
  106. _logger.LogInformation("User changed their password successfully.");
  107. StatusMessage = "Your password has been changed.";
  108. return RedirectToPage();
  109. }
  110. }
  111. }