|
| 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.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Authorization; |
| 10 | +using Microsoft.AspNetCore.Identity; |
| 11 | +using Microsoft.AspNetCore.Mvc; |
| 12 | +using Microsoft.AspNetCore.Mvc.RazorPages; |
| 13 | +using Microsoft.AspNetCore.WebUtilities; |
| 14 | +using MyApp.Data; |
| 15 | + |
| 16 | +namespace MyApp.Areas.Identity.Pages.Account |
| 17 | +{ |
| 18 | + public class ConfirmEmailModel : PageModel |
| 19 | + { |
| 20 | + private readonly UserManager<ApplicationUser> _userManager; |
| 21 | + |
| 22 | + public ConfirmEmailModel(UserManager<ApplicationUser> userManager) |
| 23 | + { |
| 24 | + _userManager = userManager; |
| 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 | + public async Task<IActionResult> OnGetAsync(string userId, string code) |
| 34 | + { |
| 35 | + if (userId == null || code == null) |
| 36 | + { |
| 37 | + return RedirectToPage("/Index"); |
| 38 | + } |
| 39 | + |
| 40 | + var user = await _userManager.FindByIdAsync(userId); |
| 41 | + if (user == null) |
| 42 | + { |
| 43 | + return NotFound($"Unable to load user with ID '{userId}'."); |
| 44 | + } |
| 45 | + |
| 46 | + code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code)); |
| 47 | + var result = await _userManager.ConfirmEmailAsync(user, code); |
| 48 | + StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email."; |
| 49 | + return Page(); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments