@page "/Account/LoginWithRecoveryCode" @using System.ComponentModel.DataAnnotations @using Microsoft.AspNetCore.Identity @using adminSystem.UI.Data @inject SignInManager SignInManager @inject UserManager UserManager @inject IdentityRedirectManager RedirectManager @inject ILogger Logger Recovery code verification

Recovery code verification


You have requested to log in with a recovery code. This login will not be remembered until you provide an authenticator app code at log in or disable 2FA and log in again.

@code { private string? message; private ApplicationUser user = default!; [SupplyParameterFromForm] private InputModel Input { get; set; } = new(); [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } protected override async Task OnInitializedAsync() { // Ensure the user has gone through the username & password screen first user = await SignInManager.GetTwoFactorAuthenticationUserAsync() ?? throw new InvalidOperationException("Unable to load two-factor authentication user."); } private async Task OnValidSubmitAsync() { var recoveryCode = Input.RecoveryCode.Replace(" ", string.Empty); var result = await SignInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode); var userId = await UserManager.GetUserIdAsync(user); if (result.Succeeded) { Logger.LogInformation("User with ID '{UserId}' logged in with a recovery code.", userId); RedirectManager.RedirectTo(ReturnUrl); } else if (result.IsLockedOut) { Logger.LogWarning("User account locked out."); RedirectManager.RedirectTo("Account/Lockout"); } else { Logger.LogWarning("Invalid recovery code entered for user with ID '{UserId}' ", userId); message = "Error: Invalid recovery code entered."; } } private sealed class InputModel { [Required] [DataType(DataType.Text)] [Display(Name = "Recovery Code")] public string RecoveryCode { get; set; } = ""; } }