A minimal ASP.NET Core project demonstrating Two-Factor Authentication (2FA) using Email with Identity.
Perfect for learning or as a starting point for more advanced authentication flows.
- User registration
- Email confirmation for first login
- Two-Factor Authentication (2FA) via email
- Simple and minimal setup with ASP.NET Core Identity
- .NET 7 or later
- PostgreSQL database (or any EF Core compatible database)
- SMTP server (for sending verification codes, e.g., MailKit)
- Clone the repository
git clone https://github.com/3raffat/BareMinimum2FAWithIdentity.git
- Configure Database and Email
Edit appsettings.Development.json:
{
}
"ConnectionStrings": {
"DefaultConnection": ""
},
"JwtSettings": {
"Secret": "",
"TokenExpirationInMinutes": 0,
"Issuer": "",
"Audience": ""
},
"EmailSettings": {
"From": "",
"SmtpHost": "",
"Port": 0,
"Username": "",
"Password": ""
}
}3.Install NuGet Packages
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package MailKit4.Run Migrations
dotnet ef database update5.Run the Application
dotnet run1.User Registration
Users sign up with email and password. A confirmation email is sent for first login.
2.Email Confirmation
Users confirm their account by clicking the link or entering the code.
3.Two-Factor Authentication (2FA)
After confirmation, login requires a 2FA code sent to email.
4.Subsequent Logins
Verified users can log in normally with 2FA.
Bare minimum example: only supports email-based 2FA.
No QR codes or Authenticator apps included.
Can be extended to SMS or app-based 2FA.
Using PostgreSQL instead of SQL Server, ensure Npgsql provider is installed.