This project demonstrates how to implement JWT (JSON Web Token) based authentication using ASP.NET Core and Entity Framework Core.
- 🔐 User registration and login with JWT access tokens
- 🔄 Token refresh mechanism (access + refresh tokens)
- 🧑💼 Role-based authorization
- 💾 PostgreSQL with Entity Framework Core
- 🛠️ Docker support using
Dockerfile - 🗃️ Database migrations via EF Core
- 🌍 CQRS enabled for frontend communication
| Technology | Description |
|---|---|
| ASP.NET Core | .NET 8 / Web API |
| EF Core | PostgreSQL database ORM |
| PostgreSQL | Main relational database |
| JWT | Access and Refresh Token based auth |
| Serilog | Structured logging to file |
| Dockerfile | For containerizing the API |
| Swagger | API documentation and testing |
- .NET 8 SDK
- Docker
- PostgreSQL (running locally or on server)
- Clone the repository
git clone https://github.com/Martik2002/JWTAuthentication.git
cd JWTAuthentication-
Update
appsettings.Development.json
Provide correct PostgreSQL connection string. -
Apply Migrations
dotnet ef database update- Run the API
dotnet runDefault port:
http://localhost:5000
- Build the Docker image
docker build -t jwt-auth-api .- Run the Docker container
docker run -d -p 5000:80 --name jwt-auth-api jwt-auth-apiMake sure PostgreSQL is accessible from the container.
Once running, go to:
http://localhost:5000/swagger/index.html
You can test:
POST /api/Auth/registerPOST /api/Auth/loginPOST /api/Auth/refresh
- Register →
/api/Auth/register - Login →
/api/Auth/login
→ ReturnsaccessToken+refreshToken - Use token in protected requests
Authorization: Bearer {token} - Refresh token →
/api/Auth/refresh
Serilog is used for logging with file output:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("Logs/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();Logs are saved in Logs/log.txt.
- Register a new user
- Login and copy
accessToken - Use in request header:
Authorization: Bearer {your_access_token}
- Test protected endpoint
├── Controllers/
├── Services/
├── Models/
├── Middleware/
├── Settings/
├── Program.cs
├── appsettings.json
├── Dockerfile
├── README.md
- Add Unit Tests
- Implement Email Confirmation
- Password Reset functionality
- Enable HTTPS support in Docker
This project is licensed under the MIT License.
- Martik Avagyan – GitHub Profile