This project implements Advanced Multi-Factor Authentication (MFA) in .NET using Domain-Driven Design (DDD) and Clean Architecture. It provides enterprise-grade security with multiple authentication factors, device management, rate limiting, and more.
✅ User Registration – Secure account creation with password hashing.
✅ Token-Based Authentication – Standard JWT token issuance upon login.
✅ Multiple MFA Methods – TOTP, Backup Codes, Recovery Codes
✅ QR Code Setup – Easy TOTP app integration with QR codes
✅ Device Management – Track and trust devices
✅ Rate Limiting – Protection against brute-force attacks
✅ Session Management – Secure session handling
✅ MFA Setup Wizard – Guided MFA configuration
✅ API Documentation – NSwag integration for Swagger UI
✅ Domain-Driven Design (DDD) & Clean Architecture – Modular, maintainable, and scalable.
- Traditional method from the base implementation
- Industry standard TOTP implementation (Google Authenticator, Authy, etc.)
- QR code setup for easy configuration
- 6-digit codes refreshed every 30 seconds
- 10 single-use backup codes for account recovery
- Format: XXXX-XXXX
- Regeneratable at any time
- A new user registers via the
/registerendpoint with email and password.
- User logs in via
/loginendpoint. - If MFA is disabled, a JWT token is issued.
- If MFA is enabled, login is blocked, and the user must use MFA.
- User calls the
/mfa-setup-wizardendpoint. - System automatically:
- Enables MFA if not already enabled
- Generates recovery code
- Sets up TOTP with QR code
- Generates 10 backup codes
- Returns all information in a single response
User can authenticate using any of these methods:
- TOTP Code via
/validate-totp - Backup Code via
/validate-backup-code - Recovery Code via
/login-with-mfa
- Track devices used for authentication
- Trust devices to bypass MFA for a period
- Manage trusted devices via API
- User calls
/disable-mfaendpoint. - All MFA methods are disabled, and the user can log in normally.
🔹 .NET Core – Secure authentication framework.
🔹 JWT Authentication – Token-based security mechanism.
🔹 Domain-Driven Design (DDD) – Structured, scalable architecture.
🔹 Clean Architecture – Separation of concerns for maintainability.
🔹 Otp.NET – TOTP implementation for time-based codes
🔹 NSwag – API documentation and Swagger UI
🔹 Fluent Validation – Ensuring strong input validation.
🔹 Serilog – Centralized logging for security auditing.
🔹 MediatR – Clean command/query handling
Before using this system, ensure you have:
✅ .NET SDK installed
✅ A database for user authentication
✅ Postman or Swagger UI for API testing
git clone https://github.com/MrEshboboyev/multi-factor-authentification.git
cd multi-factor-authentification dotnet restore dotnet run When running the application in Development mode, you can access the Swagger UI at:
https://localhost:5001/swagger
The OpenAPI specification is available at:
https://localhost:5001/swagger/v1/swagger.json
| Method | Endpoint | Description |
|---|---|---|
| POST | /register |
Registers a new user |
| POST | /login |
Logs in a user (JWT issued if MFA is disabled) |
| POST | /login-with-mfa |
Logs in a user with recovery code |
| POST | /enable-mfa |
Enables MFA and generates a recovery code |
| POST | /disable-mfa |
Disables MFA for the user |
| POST | /setup-totp |
Sets up TOTP and returns QR code URL |
| POST | /validate-totp |
Validates TOTP code and issues JWT |
| POST | /generate-backup-codes |
Generates new backup codes |
| POST | /validate-backup-code |
Validates backup code and issues JWT |
| POST | /mfa-setup-wizard |
Complete MFA setup in one call |
| POST | /manage-trusted-device |
Trust/Untrust devices |
✅ Multiple Authentication Factors – TOTP, Backup Codes, Recovery Codes
✅ Rate Limiting – Account lockout after 5 failed attempts
✅ Device Tracking – Monitor authentication devices
✅ Session Management – Secure session handling
✅ JWT-Based Authentication – Secure token issuance and validation.
✅ Password Hashing – User passwords are encrypted before storage.
✅ Input Validation – Strong validation on all endpoints
✅ Unit Testing – Covers authentication and security workflows.
✅ API Testing – Supports Postman, Swagger UI, and automated tests.
✅ Security Testing – Rate limiting, brute force protection.
✅ Logging & Monitoring – Tracks login attempts and security events.
✅ Enterprise-Grade Security – Multiple MFA methods and protections
✅ Scalable & Maintainable – Built using DDD & Clean Architecture.
✅ Ready for Production – Implements industry security best practices.
✅ Developer Friendly – Clean APIs and comprehensive documentation
✅ Extensible – Easy to add new authentication methods
Developed by MrEshboboyev, a .NET expert specializing in secure authentication, clean architectures, and enterprise software development.
This project is licensed under the MIT License. Feel free to use and contribute!
🚀 Ready to implement advanced MFA in your .NET applications? Clone the repo and start securing your users today!