source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/ConfirmEmailChange.cshtml.cs@ 99d0ecc

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

init commit Elena

  • Property mode set to 100644
File size: 2.5 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.Text;
7using System.Threading.Tasks;
8using Microsoft.AspNetCore.Authorization;
9using Microsoft.AspNetCore.Identity;
10using Microsoft.AspNetCore.Mvc;
11using Microsoft.AspNetCore.Mvc.RazorPages;
12using Microsoft.AspNetCore.WebUtilities;
13
14namespace 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}
Note: See TracBrowser for help on using the repository browser.