ConfirmEmail.cshtml.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. using System;
  5. using System.Linq;
  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. namespace bitforum.Areas.Identity.Pages.Account
  14. {
  15. public class ConfirmEmailModel : PageModel
  16. {
  17. private readonly UserManager<IdentityUser> _userManager;
  18. public ConfirmEmailModel(UserManager<IdentityUser> userManager)
  19. {
  20. _userManager = userManager;
  21. }
  22. /// <summary>
  23. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  24. /// directly from your code. This API may change or be removed in future releases.
  25. /// </summary>
  26. [TempData]
  27. public string StatusMessage { get; set; }
  28. public async Task<IActionResult> OnGetAsync(string userId, string code)
  29. {
  30. if (userId == null || code == null)
  31. {
  32. return RedirectToPage("/Index");
  33. }
  34. var user = await _userManager.FindByIdAsync(userId);
  35. if (user == null)
  36. {
  37. return NotFound($"Unable to load user with ID '{userId}'.");
  38. }
  39. code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
  40. var result = await _userManager.ConfirmEmailAsync(user, code);
  41. StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
  42. return Page();
  43. }
  44. }
  45. }