RegisterConfirmation.cshtml.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Identity;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.AspNetCore.WebUtilities;
  8. namespace BO.AppServer.Web.Areas.Identity.Pages.Account
  9. {
  10. [AllowAnonymous]
  11. public class RegisterConfirmationModel : PageModel
  12. {
  13. private readonly UserManager<IdentityUser> _userManager;
  14. public RegisterConfirmationModel(UserManager<IdentityUser> userManager)
  15. {
  16. _userManager = userManager;
  17. }
  18. public string Email { get; set; }
  19. public bool DisplayConfirmAccountLink { get; set; }
  20. public string EmailConfirmationUrl { get; set; }
  21. public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
  22. {
  23. if (email == null)
  24. {
  25. return RedirectToPage("/Index");
  26. }
  27. var user = await _userManager.FindByEmailAsync(email);
  28. if (user == null)
  29. {
  30. return NotFound($"Unable to load user with email '{email}'.");
  31. }
  32. Email = email;
  33. // Once you add a real email sender, you should remove this code that lets you confirm the account
  34. DisplayConfirmAccountLink = true;
  35. if (DisplayConfirmAccountLink)
  36. {
  37. var userId = await _userManager.GetUserIdAsync(user);
  38. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  39. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  40. EmailConfirmationUrl = Url.Page(
  41. "/Account/ConfirmEmail",
  42. pageHandler: null,
  43. values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
  44. protocol: Request.Scheme);
  45. }
  46. return Page();
  47. }
  48. }
  49. }