LoginWithRecoveryCode.cshtml.cs 4.3 KB

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