Advertisement
Intermediate ASP.NET Core, Authentication

How does two-factor authentication (2FA) work with ASP.NET Core Identity, and what does the sign-in flow look like?

Short answer

Identity's SignInManager.PasswordSignInAsync returns a result indicating whether 2FA is required (RequiresTwoFactor), at which point the user is redirected to a verification step (typically an authenticator app TOTP code or an SMS/email code) rather than being immediately signed…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Identity's SignInManager.PasswordSignInAsync returns a result indicating whether 2FA is required (RequiresTwoFactor), at which point the user is redirected to a verification step (typically an authenticator app TOTP code or an SMS/email code) rather than being immediately signed in -- only after successfully verifying the second factor via SignInManager.TwoFactorAuthenticatorSignInAsync does a full authenticated session get established.
var result = await _signInManager.PasswordSignInAsync(username, password, isPersistent: false, lockoutOnFailure: true);
if (result.RequiresTwoFactor) {
    return RedirectToPage("./LoginWith2fa");
}

// On the 2FA verification page:
var twoFactorResult = await _signInManager.TwoFactorAuthenticatorSignInAsync(code, isPersistent, rememberClient: false);
Real-world example
A financial application requires 2FA for all user accounts, with Identity's built-in flow handling the entire authenticator-app-based verification sequence without custom-built session state management for the intermediate 'password verified, 2FA pending' state.
Likely follow-ups
  • How does Identity generate and validate TOTP codes without a third-party service?
  • What's the difference between 'remember this browser' and full session persistence for repeated 2FA prompts?
Related topics