A backend API for handling JWT authentication built with ASP.NET Core and Entity Framework Core. Provides secure login, registration, token refresh, role-based access, and HTTP-only cookie management.
- User registration and login
- Secure password hashing with BCrypt
- JWT token issuance and validation
- Token refresh mechanism with rotating refresh tokens
- Role-based access control
- HTTP-only cookie authentication
- Standardized API response wrapper
- Generic repository pattern
- RESTful API design
- Swagger API documentation
- Unit tests with xUnit and Moq
| Layer | Technology |
|---|---|
| Framework | ASP.NET Core (.NET 10) |
| Language | C# |
| Database | SQLite (via Entity Framework Core) |
| Authentication | JWT (JSON Web Token) |
| Testing | xUnit, Moq |
| Documentation | Swagger / Swashbuckle |
The project follows a layered architecture for clean separation of concerns.
Controller β Service β Repository β Database
- Controllers β handle HTTP requests and API responses
- Services β authentication logic, JWT generation, password hashing
- Repositories β generic data access via
IRepository<T> - Middleware β custom JWT token processing
- Models β domain entities, DTOs, and API response wrapper
JwtAuthService/
β
βββ Controllers/
β βββ AuthController.cs # Register, login, refresh, logout, profile
β
βββ Services/
β βββ AuthService.cs # JWT generation, password hashing, refresh tokens
β
βββ Repositories/
β βββ IRepository.cs # Generic repository interface
β βββ Repository.cs # Generic repository implementation
β
βββ Models/
β βββ User.cs # User entity with refresh token fields
β βββ Role.cs # Role constants
β βββ AuthDtos.cs # Request/response DTOs
β βββ ApiResponse.cs # Standardized API response wrapper
β
βββ Data/
β βββ AuthDbContext.cs # EF Core database context
β
βββ Middleware/
β βββ JwtMiddleware.cs # Custom JWT processing middleware
β
βββ Migrations/ # EF Core database migrations
β
βββ Tests/
β βββ AuthServiceTests.cs # Unit tests for AuthService
β
βββ Program.cs # App configuration, DI, CORS, JWT setup
βββ appsettings.json # App settings & connection strings
βββ README.md
- .NET SDK 10 or later
- Visual Studio, VS Code, or Rider
# Clone the repository
git clone https://github.com/muisoft/jwt-auth-service-dotnet.git
# Navigate to the project folder
cd jwt-auth-service-dotnet
# Restore dependencies
dotnet restore
# Run the application
dotnet runThe API will be available at https://localhost:5001 with Swagger UI at /swagger.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /user/register |
Register a new user | No |
| POST | /user/authenticate |
Login and receive tokens | No |
| POST | /user/refresh |
Refresh JWT token | No |
| POST | /user/signout |
Logout and clear cookies | No |
| GET | /user/me |
Get current user profile | Yes |
The API supports two authentication methods:
1. HTTP-only Cookies (recommended for browsers): Tokens are automatically set as secure HTTP-only cookies on login.
2. Bearer Token (for API clients):
Authorization: Bearer <your_jwt_token>
dotnet testThis project is licensed under the MIT License.
Muideen Adewale β Senior Software Engineer