RegisterConfirmation.cshtml.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Identity;
  9. using Microsoft.AspNetCore.Identity.UI.Services;
  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. [AllowAnonymous]
  16. public class RegisterConfirmationModel : PageModel
  17. {
  18. private readonly UserManager<IdentityUser> _userManager;
  19. private readonly IEmailSender _sender;
  20. public RegisterConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender sender)
  21. {
  22. _userManager = userManager;
  23. _sender = sender;
  24. }
  25. /// <summary>
  26. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  27. /// directly from your code. This API may change or be removed in future releases.
  28. /// </summary>
  29. public string Email { get; set; }
  30. /// <summary>
  31. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  32. /// directly from your code. This API may change or be removed in future releases.
  33. /// </summary>
  34. public bool DisplayConfirmAccountLink { get; set; }
  35. /// <summary>
  36. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  37. /// directly from your code. This API may change or be removed in future releases.
  38. /// </summary>
  39. public string EmailConfirmationUrl { get; set; }
  40. public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
  41. {
  42. if (email == null)
  43. {
  44. return RedirectToPage("/Index");
  45. }
  46. returnUrl = returnUrl ?? Url.Content("~/");
  47. var user = await _userManager.FindByEmailAsync(email);
  48. if (user == null)
  49. {
  50. return NotFound($"Unable to load user with email '{email}'.");
  51. }
  52. Email = email;
  53. // Once you add a real email sender, you should remove this code that lets you confirm the account
  54. DisplayConfirmAccountLink = true;
  55. if (DisplayConfirmAccountLink)
  56. {
  57. var userId = await _userManager.GetUserIdAsync(user);
  58. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  59. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  60. EmailConfirmationUrl = Url.Page(
  61. "/Account/ConfirmEmail",
  62. pageHandler: null,
  63. values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
  64. protocol: Request.Scheme);
  65. }
  66. return Page();
  67. }
  68. }
  69. }