ResendEmailConfirmation.cshtml.cs 3.3 KB

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