SetPassword.cshtml.cs 4.3 KB

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