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

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

init commit Elena

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