ConfirmEmail.cshtml.cs 1.8 KB

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