TwoFactorAuthentication.cshtml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Threading.Tasks;
  6. using bitforum.Models.User;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.Extensions.Logging;
  11. namespace bitforum.Areas.Identity.Pages.Account.Manage
  12. {
  13. public class TwoFactorAuthenticationModel : PageModel
  14. {
  15. private readonly UserManager<ApplicationUser> _userManager;
  16. private readonly SignInManager<ApplicationUser> _signInManager;
  17. private readonly ILogger<TwoFactorAuthenticationModel> _logger;
  18. public TwoFactorAuthenticationModel(
  19. UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, ILogger<TwoFactorAuthenticationModel> logger)
  20. {
  21. _userManager = userManager;
  22. _signInManager = signInManager;
  23. _logger = logger;
  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 bool HasAuthenticator { 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 int RecoveryCodesLeft { 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. [BindProperty]
  40. public bool Is2faEnabled { get; set; }
  41. /// <summary>
  42. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  43. /// directly from your code. This API may change or be removed in future releases.
  44. /// </summary>
  45. public bool IsMachineRemembered { get; set; }
  46. /// <summary>
  47. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  48. /// directly from your code. This API may change or be removed in future releases.
  49. /// </summary>
  50. [TempData]
  51. public string StatusMessage { get; set; }
  52. public async Task<IActionResult> OnGetAsync()
  53. {
  54. var user = await _userManager.GetUserAsync(User);
  55. if (user == null)
  56. {
  57. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  58. }
  59. HasAuthenticator = await _userManager.GetAuthenticatorKeyAsync(user) != null;
  60. Is2faEnabled = await _userManager.GetTwoFactorEnabledAsync(user);
  61. IsMachineRemembered = await _signInManager.IsTwoFactorClientRememberedAsync(user);
  62. RecoveryCodesLeft = await _userManager.CountRecoveryCodesAsync(user);
  63. return Page();
  64. }
  65. public async Task<IActionResult> OnPostAsync()
  66. {
  67. var user = await _userManager.GetUserAsync(User);
  68. if (user == null)
  69. {
  70. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  71. }
  72. await _signInManager.ForgetTwoFactorClientAsync();
  73. StatusMessage = "The current browser has been forgotten. When you login again from this browser you will be prompted for your 2fa code.";
  74. return RedirectToPage();
  75. }
  76. }
  77. }