forked from OPCFoundation/UA-CloudLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmail.cshtml.cs
201 lines (172 loc) · 8.07 KB
/
Email.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
using Opc.Ua.Cloud.Library.Authentication;
namespace Opc.Ua.Cloud.Library.Areas.Identity.Pages.Account.Manage
{
public class EmailModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly Interfaces.ICaptchaValidation _captchaValidation;
private readonly CaptchaSettings _captchaSettings;
public EmailModel(
UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager,
IEmailSender emailSender,
IConfiguration configuration,
Interfaces.ICaptchaValidation captchaValidation)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_captchaValidation = captchaValidation;
_captchaSettings = new CaptchaSettings();
configuration.GetSection("CaptchaSettings").Bind(_captchaSettings);
}
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public string Email { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public bool IsEmailConfirmed { get; set; }
/// <summary>
/// Populate values for cshtml to use
/// </summary>
public CaptchaSettings CaptchaSettings { get { return _captchaSettings; } }
/// <summary>
/// Populate a token returned from client side call to Google Captcha
/// </summary>
[BindProperty]
public string CaptchaResponseToken { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[TempData]
public string StatusMessage { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[BindProperty]
public InputModel Input { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class InputModel
{
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[Required]
[EmailAddress]
[Display(Name = "New email")]
public string NewEmail { get; set; }
}
private async Task LoadAsync(IdentityUser user)
{
var email = await _userManager.GetEmailAsync(user).ConfigureAwait(false);
Email = email;
Input = new InputModel {
NewEmail = email,
};
IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user).ConfigureAwait(false);
}
public async Task<IActionResult> OnGetAsync()
{
var user = await _userManager.GetUserAsync(User).ConfigureAwait(false);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
await LoadAsync(user).ConfigureAwait(false);
return Page();
}
public async Task<IActionResult> OnPostChangeEmailAsync()
{
var user = await _userManager.GetUserAsync(User).ConfigureAwait(false);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
var captchaResult = await _captchaValidation.ValidateCaptcha(CaptchaResponseToken);
if (!string.IsNullOrEmpty(captchaResult)) ModelState.AddModelError("CaptchaResponseToken", captchaResult);
if (!ModelState.IsValid)
{
await LoadAsync(user);
return Page();
}
var email = await _userManager.GetEmailAsync(user).ConfigureAwait(false);
if (Input.NewEmail != email)
{
var userId = await _userManager.GetUserIdAsync(user).ConfigureAwait(false);
var code = await _userManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail).ConfigureAwait(false);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmailChange",
pageHandler: null,
values: new { area = "Identity", userId = userId, email = Input.NewEmail, code = code },
protocol: Request.Scheme);
await EmailManager.SendConfirmEmailChange(
_emailSender,
Input.NewEmail,
callbackUrl
).ConfigureAwait(false);
StatusMessage = "Confirmation link to change email sent. Please check your email.";
return RedirectToPage();
}
StatusMessage = "Your email is unchanged.";
return RedirectToPage();
}
public async Task<IActionResult> OnPostSendVerificationEmailAsync()
{
var user = await _userManager.GetUserAsync(User).ConfigureAwait(false);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
//Captcha validate
var captchaResult = await _captchaValidation.ValidateCaptcha(CaptchaResponseToken);
if (!string.IsNullOrEmpty(captchaResult)) ModelState.AddModelError("CaptchaResponseToken", captchaResult);
if (!ModelState.IsValid)
{
await LoadAsync(user).ConfigureAwait(false);
return Page();
}
var userId = await _userManager.GetUserIdAsync(user).ConfigureAwait(false);
var email = await _userManager.GetEmailAsync(user).ConfigureAwait(false);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code },
protocol: Request.Scheme);
await EmailManager.SendReconfirmEmail(
_emailSender,
Input.NewEmail,
callbackUrl
).ConfigureAwait(false);
StatusMessage = "Verification email sent. Please check your email.";
return RedirectToPage();
}
}
}