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 |
|
---|
5 | using System;
|
---|
6 | using System.ComponentModel.DataAnnotations;
|
---|
7 | using System.Text;
|
---|
8 | using System.Text.Encodings.Web;
|
---|
9 | using System.Threading.Tasks;
|
---|
10 | using Microsoft.AspNetCore.Identity;
|
---|
11 | using Microsoft.AspNetCore.Identity.UI.Services;
|
---|
12 | using Microsoft.AspNetCore.Mvc;
|
---|
13 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
14 | using Microsoft.AspNetCore.WebUtilities;
|
---|
15 |
|
---|
16 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
|
---|
17 | {
|
---|
18 | public class EmailModel : PageModel
|
---|
19 | {
|
---|
20 | private readonly UserManager<IdentityUser> _userManager;
|
---|
21 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
22 | private readonly IEmailSender _emailSender;
|
---|
23 |
|
---|
24 | public EmailModel(
|
---|
25 | UserManager<IdentityUser> userManager,
|
---|
26 | SignInManager<IdentityUser> signInManager,
|
---|
27 | IEmailSender emailSender)
|
---|
28 | {
|
---|
29 | _userManager = userManager;
|
---|
30 | _signInManager = signInManager;
|
---|
31 | _emailSender = emailSender;
|
---|
32 | }
|
---|
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 | public string Email { get; set; }
|
---|
39 |
|
---|
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 bool IsEmailConfirmed { get; set; }
|
---|
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 | [TempData]
|
---|
51 | public string StatusMessage { get; set; }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
55 | /// directly from your code. This API may change or be removed in future releases.
|
---|
56 | /// </summary>
|
---|
57 | [BindProperty]
|
---|
58 | public InputModel Input { 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 | public class InputModel
|
---|
65 | {
|
---|
66 | /// <summary>
|
---|
67 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
68 | /// directly from your code. This API may change or be removed in future releases.
|
---|
69 | /// </summary>
|
---|
70 | [Required]
|
---|
71 | [EmailAddress]
|
---|
72 | [Display(Name = "New email")]
|
---|
73 | public string NewEmail { get; set; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private async Task LoadAsync(IdentityUser user)
|
---|
77 | {
|
---|
78 | var email = await _userManager.GetEmailAsync(user);
|
---|
79 | Email = email;
|
---|
80 |
|
---|
81 | Input = new InputModel
|
---|
82 | {
|
---|
83 | NewEmail = email,
|
---|
84 | };
|
---|
85 |
|
---|
86 | IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public async Task<IActionResult> OnGetAsync()
|
---|
90 | {
|
---|
91 | var user = await _userManager.GetUserAsync(User);
|
---|
92 | if (user == null)
|
---|
93 | {
|
---|
94 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
95 | }
|
---|
96 |
|
---|
97 | await LoadAsync(user);
|
---|
98 | return Page();
|
---|
99 | }
|
---|
100 |
|
---|
101 | public async Task<IActionResult> OnPostChangeEmailAsync()
|
---|
102 | {
|
---|
103 | var user = await _userManager.GetUserAsync(User);
|
---|
104 | if (user == null)
|
---|
105 | {
|
---|
106 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (!ModelState.IsValid)
|
---|
110 | {
|
---|
111 | await LoadAsync(user);
|
---|
112 | return Page();
|
---|
113 | }
|
---|
114 |
|
---|
115 | var email = await _userManager.GetEmailAsync(user);
|
---|
116 | if (Input.NewEmail != email)
|
---|
117 | {
|
---|
118 | var userId = await _userManager.GetUserIdAsync(user);
|
---|
119 | var code = await _userManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail);
|
---|
120 | code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
|
---|
121 | var callbackUrl = Url.Page(
|
---|
122 | "/Account/ConfirmEmailChange",
|
---|
123 | pageHandler: null,
|
---|
124 | values: new { area = "Identity", userId = userId, email = Input.NewEmail, code = code },
|
---|
125 | protocol: Request.Scheme);
|
---|
126 | await _emailSender.SendEmailAsync(
|
---|
127 | Input.NewEmail,
|
---|
128 | "Confirm your email",
|
---|
129 | $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
|
---|
130 |
|
---|
131 | StatusMessage = "Confirmation link to change email sent. Please check your email.";
|
---|
132 | return RedirectToPage();
|
---|
133 | }
|
---|
134 |
|
---|
135 | StatusMessage = "Your email is unchanged.";
|
---|
136 | return RedirectToPage();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public async Task<IActionResult> OnPostSendVerificationEmailAsync()
|
---|
140 | {
|
---|
141 | var user = await _userManager.GetUserAsync(User);
|
---|
142 | if (user == null)
|
---|
143 | {
|
---|
144 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (!ModelState.IsValid)
|
---|
148 | {
|
---|
149 | await LoadAsync(user);
|
---|
150 | return Page();
|
---|
151 | }
|
---|
152 |
|
---|
153 | var userId = await _userManager.GetUserIdAsync(user);
|
---|
154 | var email = await _userManager.GetEmailAsync(user);
|
---|
155 | var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
---|
156 | code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
|
---|
157 | var callbackUrl = Url.Page(
|
---|
158 | "/Account/ConfirmEmail",
|
---|
159 | pageHandler: null,
|
---|
160 | values: new { area = "Identity", userId = userId, code = code },
|
---|
161 | protocol: Request.Scheme);
|
---|
162 | await _emailSender.SendEmailAsync(
|
---|
163 | email,
|
---|
164 | "Confirm your email",
|
---|
165 | $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
|
---|
166 |
|
---|
167 | StatusMessage = "Verification email sent. Please check your email.";
|
---|
168 | return RedirectToPage();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|