LoginWithRecoveryCode.cshtml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Identity;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.Mvc.RazorPages;
  11. using Microsoft.Extensions.Logging;
  12. namespace bitforum.Areas.Identity.Pages.Account
  13. {
  14. public class LoginWithRecoveryCodeModel : PageModel
  15. {
  16. private readonly SignInManager<IdentityUser> _signInManager;
  17. private readonly UserManager<IdentityUser> _userManager;
  18. private readonly ILogger<LoginWithRecoveryCodeModel> _logger;
  19. public LoginWithRecoveryCodeModel(
  20. SignInManager<IdentityUser> signInManager,
  21. UserManager<IdentityUser> userManager,
  22. ILogger<LoginWithRecoveryCodeModel> logger)
  23. {
  24. _signInManager = signInManager;
  25. _userManager = userManager;
  26. _logger = logger;
  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 string ReturnUrl { get; set; }
  39. /// <summary>
  40. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  41. /// directly from your code. This API may change or be removed in future releases.
  42. /// </summary>
  43. public class InputModel
  44. {
  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. [BindProperty]
  50. [Required]
  51. [DataType(DataType.Text)]
  52. [Display(Name = "Recovery Code")]
  53. public string RecoveryCode { get; set; }
  54. }
  55. public async Task<IActionResult> OnGetAsync(string returnUrl = null)
  56. {
  57. // Ensure the user has gone through the username & password screen first
  58. var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
  59. if (user == null)
  60. {
  61. throw new InvalidOperationException($"Unable to load two-factor authentication user.");
  62. }
  63. ReturnUrl = returnUrl;
  64. return Page();
  65. }
  66. public async Task<IActionResult> OnPostAsync(string returnUrl = null)
  67. {
  68. if (!ModelState.IsValid)
  69. {
  70. return Page();
  71. }
  72. var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
  73. if (user == null)
  74. {
  75. throw new InvalidOperationException($"Unable to load two-factor authentication user.");
  76. }
  77. var recoveryCode = Input.RecoveryCode.Replace(" ", string.Empty);
  78. var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode);
  79. var userId = await _userManager.GetUserIdAsync(user);
  80. if (result.Succeeded)
  81. {
  82. _logger.LogInformation("User with ID '{UserId}' logged in with a recovery code.", user.Id);
  83. return LocalRedirect(returnUrl ?? Url.Content("~/"));
  84. }
  85. if (result.IsLockedOut)
  86. {
  87. _logger.LogWarning("User account locked out.");
  88. return RedirectToPage("./Lockout");
  89. }
  90. else
  91. {
  92. _logger.LogWarning("Invalid recovery code entered for user with ID '{UserId}' ", user.Id);
  93. ModelState.AddModelError(string.Empty, "Invalid recovery code entered.");
  94. return Page();
  95. }
  96. }
  97. }
  98. }