Skip to content

About

πŸ” JWT Authentication Service β€” ASP.NET Core (.NET 10) with token refresh, role-based access, HTTP-only cookies, generic repository pattern, and EF Core SQLite.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

JWT Authentication Service

.NET License Build

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.


πŸš€ Features

  • 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

🧰 Tech Stack

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

πŸ— Architecture

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

πŸ“‚ Project Structure

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

βš™οΈ Getting Started

Prerequisites

  • .NET SDK 10 or later
  • Visual Studio, VS Code, or Rider

Installation

# 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 run

The API will be available at https://localhost:5001 with Swagger UI at /swagger.


πŸ“˜ API Endpoints

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

πŸ” Authentication

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>

πŸ§ͺ Running Tests

dotnet test

πŸ“„ License

This project is licensed under the MIT License.


πŸ‘€ Author

Muideen Adewale β€” Senior Software Engineer

LinkedIn Email

About

πŸ” JWT Authentication Service β€” ASP.NET Core (.NET 10) with token refresh, role-based access, HTTP-only cookies, generic repository pattern, and EF Core SQLite.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages