ResendEmailConfirmation.cshtml.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.ComponentModel.DataAnnotations;
  6. using System.Text;
  7. using System.Text.Encodings.Web;
  8. using System.Threading.Tasks;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Identity;
  11. using Microsoft.AspNetCore.Identity.UI.Services;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.AspNetCore.Mvc.RazorPages;
  14. using Microsoft.AspNetCore.WebUtilities;
  15. namespace bitforum.Areas.Identity.Pages.Account
  16. {
  17. [AllowAnonymous]
  18. public class ResendEmailConfirmationModel : PageModel
  19. {
  20. private readonly UserManager<IdentityUser> _userManager;
  21. private readonly IEmailSender _emailSender;
  22. public ResendEmailConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender emailSender)
  23. {
  24. _userManager = userManager;
  25. _emailSender = emailSender;
  26. }
  27. /// <summary>
  28. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  29. /// directly from your code. This API may change or be removed in future releases.
  30. /// </summary>
  31. [BindProperty]
  32. public InputModel Input { get; set; }
  33. /// <summary>
  34. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  35. /// directly from your code. This API may change or be removed in future releases.
  36. /// </summary>
  37. public class InputModel
  38. {
  39. /// <summary>
  40. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  41. /// directly from your code. This API may change or be removed in future releases.
  42. /// </summary>
  43. [Required]
  44. [EmailAddress]
  45. public string Email { get; set; }
  46. }
  47. public void OnGet()
  48. {
  49. }
  50. public async Task<IActionResult> OnPostAsync()
  51. {
  52. if (!ModelState.IsValid)
  53. {
  54. return Page();
  55. }
  56. var user = await _userManager.FindByEmailAsync(Input.Email);
  57. if (user == null)
  58. {
  59. ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
  60. return Page();
  61. }
  62. var userId = await _userManager.GetUserIdAsync(user);
  63. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  64. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  65. var callbackUrl = Url.Page(
  66. "/Account/ConfirmEmail",
  67. pageHandler: null,
  68. values: new { userId = userId, code = code },
  69. protocol: Request.Scheme);
  70. await _emailSender.SendEmailAsync(
  71. Input.Email,
  72. "Confirm your email",
  73. $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
  74. ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
  75. return Page();
  76. }
  77. }
  78. }