RegisterConfirmation.cshtml.cs 3.1 KB

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