source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/SetPassword.cshtml.cs

main
Last change on this file was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 2 months ago

init commit Elena

  • Property mode set to 100644
File size: 4.3 KB
Line 
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
5using System;
6using System.ComponentModel.DataAnnotations;
7using System.Threading.Tasks;
8using Microsoft.AspNetCore.Identity;
9using Microsoft.AspNetCore.Mvc;
10using Microsoft.AspNetCore.Mvc.RazorPages;
11
12namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
13{
14 public class SetPasswordModel : PageModel
15 {
16 private readonly UserManager<IdentityUser> _userManager;
17 private readonly SignInManager<IdentityUser> _signInManager;
18
19 public SetPasswordModel(
20 UserManager<IdentityUser> userManager,
21 SignInManager<IdentityUser> signInManager)
22 {
23 _userManager = userManager;
24 _signInManager = signInManager;
25 }
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
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
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 public class InputModel
46 {
47 /// <summary>
48 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
49 /// directly from your code. This API may change or be removed in future releases.
50 /// </summary>
51 [Required]
52 [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
53 [DataType(DataType.Password)]
54 [Display(Name = "New password")]
55 public string NewPassword { get; set; }
56
57 /// <summary>
58 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
59 /// directly from your code. This API may change or be removed in future releases.
60 /// </summary>
61 [DataType(DataType.Password)]
62 [Display(Name = "Confirm new password")]
63 [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
64 public string ConfirmPassword { get; set; }
65 }
66
67 public async Task<IActionResult> OnGetAsync()
68 {
69 var user = await _userManager.GetUserAsync(User);
70 if (user == null)
71 {
72 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
73 }
74
75 var hasPassword = await _userManager.HasPasswordAsync(user);
76
77 if (hasPassword)
78 {
79 return RedirectToPage("./ChangePassword");
80 }
81
82 return Page();
83 }
84
85 public async Task<IActionResult> OnPostAsync()
86 {
87 if (!ModelState.IsValid)
88 {
89 return Page();
90 }
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
98 var addPasswordResult = await _userManager.AddPasswordAsync(user, Input.NewPassword);
99 if (!addPasswordResult.Succeeded)
100 {
101 foreach (var error in addPasswordResult.Errors)
102 {
103 ModelState.AddModelError(string.Empty, error.Description);
104 }
105 return Page();
106 }
107
108 await _signInManager.RefreshSignInAsync(user);
109 StatusMessage = "Your password has been set.";
110
111 return RedirectToPage();
112 }
113 }
114}
Note: See TracBrowser for help on using the repository browser.