A practical guide to implementing TOTP-based Two-Factor Authentication in ASP.NET Core. Covers how TOTP works, generating cryptographically secure secret keys with Otp.NET, creating QR codes using QRCoder, the correct two-step setup flow (pending → confirmed), issuing limited-scope tokens during login, validating codes with replay attack prevention via time-step tracking, rate limiting the validation endpoint, encrypting TOTP secrets at rest using AES with a key vault, and generating hashed one-time recovery codes.
Table of contents
How TOTP WorksGenerating the Secret KeyCreating the QR CodeThe Setup FlowThe Login Flow With 2FAValidating TOTP CodesEncrypting Secrets at RestRecovery CodesPutting It All TogetherSummarySort: