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.Text;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using Microsoft.AspNetCore.Authorization;
|
---|
9 | using Microsoft.AspNetCore.Identity;
|
---|
10 | using Microsoft.AspNetCore.Mvc;
|
---|
11 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
12 | using Microsoft.AspNetCore.WebUtilities;
|
---|
13 |
|
---|
14 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account
|
---|
15 | {
|
---|
16 | public class ConfirmEmailChangeModel : PageModel
|
---|
17 | {
|
---|
18 | private readonly UserManager<IdentityUser> _userManager;
|
---|
19 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
20 |
|
---|
21 | public ConfirmEmailChangeModel(UserManager<IdentityUser> userManager, 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 | [TempData]
|
---|
32 | public string StatusMessage { get; set; }
|
---|
33 |
|
---|
34 | public async Task<IActionResult> OnGetAsync(string userId, string email, string code)
|
---|
35 | {
|
---|
36 | if (userId == null || email == null || code == null)
|
---|
37 | {
|
---|
38 | return RedirectToPage("/Index");
|
---|
39 | }
|
---|
40 |
|
---|
41 | var user = await _userManager.FindByIdAsync(userId);
|
---|
42 | if (user == null)
|
---|
43 | {
|
---|
44 | return NotFound($"Unable to load user with ID '{userId}'.");
|
---|
45 | }
|
---|
46 |
|
---|
47 | code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
|
---|
48 | var result = await _userManager.ChangeEmailAsync(user, email, code);
|
---|
49 | if (!result.Succeeded)
|
---|
50 | {
|
---|
51 | StatusMessage = "Error changing email.";
|
---|
52 | return Page();
|
---|
53 | }
|
---|
54 |
|
---|
55 | // In our UI email and user name are one and the same, so when we update the email
|
---|
56 | // we need to update the user name.
|
---|
57 | var setUserNameResult = await _userManager.SetUserNameAsync(user, email);
|
---|
58 | if (!setUserNameResult.Succeeded)
|
---|
59 | {
|
---|
60 | StatusMessage = "Error changing user name.";
|
---|
61 | return Page();
|
---|
62 | }
|
---|
63 |
|
---|
64 | await _signInManager.RefreshSignInAsync(user);
|
---|
65 | StatusMessage = "Thank you for confirming your email change.";
|
---|
66 | return Page();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|