ChangePassword.cshtml.cs 5.0 KB

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