A secure and scalable JWT Token Refresh Mechanism built with .NET 9, Domain-Driven Design (DDD), and Clean Architecture. Implements refresh tokens, token revocation, and multi-layered authentication to enhance security and user management.
โ
User Registration & Login โ Secure user authentication with password hashing.
โ
JWT Access Tokens โ Short-lived access tokens for secure API access.
โ
Refresh Tokens โ Long-lived refresh tokens for session continuation.
โ
Token Revocation โ Prevent unauthorized access by revoking refresh tokens.
โ
DDD & Clean Architecture โ Ensures maintainability and scalability.
โ
Unit Testing โ Comprehensive tests for security and authentication workflows.
๐ฃ .NET 9 โ Backend framework for secure API development.
๐ด JWT Authentication โ Secure user authentication and authorization.
๐ข Entity Framework Core โ Database interactions and migrations.
๐ก FluentValidation โ Request validation to enforce strong security rules.
๐ต MediatR โ Implements CQRS for command-query separation.
๐ xUnit & Moq โ Unit testing and mocking dependencies for reliability.
This project follows Clean Architecture principles and DDD:
1๏ธโฃ Domain Layer
๐ Business logic, entities, and domain events (User, RefreshToken).
2๏ธโฃ Application Layer
๐ Use cases, commands, queries (LoginCommand, RefreshTokenCommand).
3๏ธโฃ Infrastructure Layer
๐ Database, repositories, JWT provider, external services (EF Core, TokenService).
4๏ธโฃ Presentation Layer
๐ API controllers, request/response models (UsersController, LoginRequest).
๐น Users register with email and password.
๐น Passwords are hashed before being stored securely.
๐น Users log in with credentials.
๐น A JWT access token (short-lived) and a refresh token (long-lived) are issued.
๐น The refresh token is stored securely in the database.
๐น When the access token expires, the client sends a refresh token to /refresh-token
.
๐น A new JWT access token is issued.
๐น The old refresh token is revoked, ensuring security.
๐น Users can revoke refresh tokens, logging out from all devices.
๐ .NET 9 SDK
๐ Visual Studio 2022 or VS Code
๐ Postman for API testing
git clone https://github.com/MrEshboboyev/jwt-token-refresh-mechanism.git
cd jwt-token-refresh-mechanism
dotnet restore
dotnet run --project src/Presentation
Method | Endpoint | Description |
---|---|---|
POST | /api/users/register |
Registers a new user |
POST | /api/users/login |
Logs in and returns access & refresh tokens |
POST | /api/users/refresh-token |
Refreshes JWT access token |
POST | /api/users/revoke-token |
Revokes a refresh token |
Run all unit tests:
dotnet test
1๏ธโฃ Register a new user
2๏ธโฃ Log in and get access & refresh tokens
3๏ธโฃ Use refresh token to obtain a new access token
4๏ธโฃ Revoke refresh token to log out
โ Test using Postman, Swagger, or any REST client.
โ
High Security โ Implements refresh token rotation & revocation.
โ
Scalable & Maintainable โ Follows DDD & Clean Architecture.
โ
Ready for Production โ Follows industry best practices.
โ
Cloud-Ready โ Can be deployed on AWS, Azure, Kubernetes.
This project is licensed under the MIT License. See the LICENSE file for details.
For feedback, questions, or contributions:
๐ง Email: mreshboboyev@gmail.com
๐ป GitHub: MrEshboboyev
๐ Secure your API with a scalable JWT Token Refresh Mechanism! Clone the repo and start coding today!