https://github.com/mreshboboyev/multi-factor-authentification
This repository demonstrates a Multi-Factor Authentication (MFA) system in .NET, built using Domain-Driven Design (DDD) and Clean Architecture. It includes secure user registration, token-based authentication, MFA activation, recovery codes, and login with MFA enforcementβenhancing security while maintaining flexibility.
https://github.com/mreshboboyev/multi-factor-authentification
c-sharp clean-architecture domain-driven-design dotnet-core jwt-authentication login-protection multi-factor-authentication recovery-codes rest-api security software-architecture
Last synced: about 1 month ago
JSON representation
This repository demonstrates a Multi-Factor Authentication (MFA) system in .NET, built using Domain-Driven Design (DDD) and Clean Architecture. It includes secure user registration, token-based authentication, MFA activation, recovery codes, and login with MFA enforcementβenhancing security while maintaining flexibility.
- Host: GitHub
- URL: https://github.com/mreshboboyev/multi-factor-authentification
- Owner: MrEshboboyev
- Created: 2025-01-31T06:17:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-10-22T19:00:07.000Z (8 months ago)
- Last Synced: 2025-10-22T20:44:22.497Z (8 months ago)
- Topics: c-sharp, clean-architecture, domain-driven-design, dotnet-core, jwt-authentication, login-protection, multi-factor-authentication, recovery-codes, rest-api, security, software-architecture
- Language: C#
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π Advanced Multi-Factor Authentication (MFA) in .NET π
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.
---
## π Key Features
β
**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.
---
## π Advanced MFA Methods
### **1οΈβ£ Recovery Codes** (Original)
- Traditional method from the base implementation
### **2οΈβ£ Time-based One-Time Passwords (TOTP)**
- Industry standard TOTP implementation (Google Authenticator, Authy, etc.)
- QR code setup for easy configuration
- 6-digit codes refreshed every 30 seconds
### **3οΈβ£ Backup Codes**
- 10 single-use backup codes for account recovery
- Format: XXXX-XXXX
- Regeneratable at any time
---
## π Enhanced MFA Workflow
### **1οΈβ£ User Registration**
- A new user registers via the `/register` endpoint with **email and password**.
### **2οΈβ£ Standard Login (No MFA)**
- User logs in via `/login` endpoint.
- **If MFA is disabled**, a **JWT token** is issued.
- **If MFA is enabled**, login is **blocked**, and the user must use MFA.
### **3οΈβ£ MFA Setup Wizard**
- User calls the `/mfa-setup-wizard` endpoint.
- 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
### **4οΈβ£ MFA Authentication Options**
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`
### **5οΈβ£ Device Management**
- Track devices used for authentication
- Trust devices to bypass MFA for a period
- Manage trusted devices via API
### **6οΈβ£ Disabling MFA**
- User calls `/disable-mfa` endpoint.
- All MFA methods are disabled, and the user can log in normally.
---
## π Technologies Used
πΉ **.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
---
## π Getting Started
### **Prerequisites**
Before using this system, ensure you have:
β
**.NET SDK installed**
β
**A database for user authentication**
β
**Postman or Swagger UI** for API testing
### **Step 1: Clone the Repository**
```bash
git clone https://github.com/MrEshboboyev/multi-factor-authentification.git
cd multi-factor-authentification
```
### **Step 2: Install Dependencies**
```bash
dotnet restore
```
### **Step 3: Run the Application**
```bash
dotnet run
```
---
## π API Documentation
### **Swagger UI**
When running the application in **Development** mode, you can access the Swagger UI at:
```
https://localhost:5001/swagger
```
### **API Documentation**
The OpenAPI specification is available at:
```
https://localhost:5001/swagger/v1/swagger.json
```
---
## π API Endpoints
| 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 |
---
## π Security Measures
β
**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
---
## π§ͺ Testing & Quality Assurance
β
**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.
---
## π₯ Why Use This Project?
β
**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
---
## π About the Author
Developed by [MrEshboboyev](https://github.com/MrEshboboyev), a **.NET expert** specializing in **secure authentication, clean architectures, and enterprise software development**.
## π License
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!