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