SetPassword.cshtml.cs 4.3 KB

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