ConfirmEmail.cshtml.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.AspNetCore.WebUtilities;
  11. using Console = HyperCube.Utils.AdvConsole;
  12. namespace HyperCube.Areas.Identity.Pages.Account
  13. {
  14. [AllowAnonymous]
  15. public class ConfirmEmailModel : PageModel
  16. {
  17. private readonly UserManager<IdentityUser> _userManager;
  18. public ConfirmEmailModel(UserManager<IdentityUser> userManager)
  19. {
  20. _userManager = userManager;
  21. }
  22. [TempData]
  23. public string StatusMessage { get; set; }
  24. public async Task<IActionResult> OnGetAsync(string userId, string code)
  25. {
  26. if (userId == null || code == null)
  27. {
  28. return RedirectToPage("/Index");
  29. }
  30. var user = await _userManager.FindByIdAsync(userId);
  31. if (user == null)
  32. {
  33. return NotFound($"Unable to load user with ID '{userId}'.");
  34. }
  35. code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
  36. var result = await _userManager.ConfirmEmailAsync(user, code);
  37. StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
  38. if (result.Succeeded)
  39. {
  40. Console.WriteLine($"Adding new confirmed user [{user.UserName}], id: [{userId}] to list!");
  41. Models.AccountModel newAcc = new();
  42. newAcc.PWDHash = user.PasswordHash;
  43. newAcc.UUID = user.Id;
  44. newAcc.Name = user.UserName;
  45. newAcc.Email = user.Email;
  46. newAcc.bsel = 1;
  47. newAcc.FillAccount();
  48. Models.AccountModel.Loaded.Add(userId, newAcc);
  49. }
  50. return Page();
  51. }
  52. }
  53. }