LoginWith2fa.cshtml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Infrastructure.Persistence.Identity;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.RazorPages;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.Extensions.Logging;
  11. using System;
  12. using System.ComponentModel.DataAnnotations;
  13. using System.Threading.Tasks;
  14. namespace Admin.Areas.Identity.Pages.Account
  15. {
  16. public class LoginWith2faModel : PageModel
  17. {
  18. private readonly SignInManager<ApplicationUser> _signInManager;
  19. private readonly UserManager<ApplicationUser> _userManager;
  20. private readonly ILogger<LoginWith2faModel> _logger;
  21. public LoginWith2faModel(
  22. SignInManager<ApplicationUser> signInManager,
  23. UserManager<ApplicationUser> userManager,
  24. ILogger<LoginWith2faModel> logger)
  25. {
  26. _signInManager = signInManager;
  27. _userManager = userManager;
  28. _logger = logger;
  29. }
  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. [BindProperty]
  35. public InputModel Input { 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 bool RememberMe { 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 string ReturnUrl { 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. public class InputModel
  51. {
  52. /// <summary>
  53. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  54. /// directly from your code. This API may change or be removed in future releases.
  55. /// </summary>
  56. [Required]
  57. [StringLength(7, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
  58. [DataType(DataType.Text)]
  59. [Display(Name = "Authenticator code")]
  60. public string TwoFactorCode { get; set; }
  61. /// <summary>
  62. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  63. /// directly from your code. This API may change or be removed in future releases.
  64. /// </summary>
  65. [Display(Name = "Remember this machine")]
  66. public bool RememberMachine { get; set; }
  67. }
  68. public async Task<IActionResult> OnGetAsync(bool rememberMe, string returnUrl = null)
  69. {
  70. // Ensure the user has gone through the username & password screen first
  71. var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
  72. if (user == null)
  73. {
  74. throw new InvalidOperationException($"Unable to load two-factor authentication user.");
  75. }
  76. ReturnUrl = returnUrl;
  77. RememberMe = rememberMe;
  78. return Page();
  79. }
  80. public async Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl = null)
  81. {
  82. if (!ModelState.IsValid)
  83. {
  84. return Page();
  85. }
  86. returnUrl = returnUrl ?? Url.Content("~/");
  87. var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
  88. if (user == null)
  89. {
  90. throw new InvalidOperationException($"Unable to load two-factor authentication user.");
  91. }
  92. var authenticatorCode = Input.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);
  93. var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, Input.RememberMachine);
  94. var userId = await _userManager.GetUserIdAsync(user);
  95. if (result.Succeeded)
  96. {
  97. _logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);
  98. return LocalRedirect(returnUrl);
  99. }
  100. else if (result.IsLockedOut)
  101. {
  102. _logger.LogWarning("User with ID '{UserId}' account locked out.", user.Id);
  103. return RedirectToPage("./Lockout");
  104. }
  105. else
  106. {
  107. _logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
  108. ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
  109. return Page();
  110. }
  111. }
  112. }
  113. }