This project demonstrates how to implement JWT (JSON Web Token) Authentication in an ASP.NET Core Web API. It includes secure endpoints, token generation, and Swagger integration for testing. It also shows how to protect API endpoints using [Authorize], and test everything through Swagger UI.
- JWT Token generation via login endpoint
- Secure API endpoints using
[Authorize] - Swagger UI integration with JWT support
- Configuration via
appsettings.json
- ASP.NET 10
- C#
- Swagger (Swashbuckle)
- JWT Bearer Authentication
- .NET Dependency Injection
JWT-Authentication-in-.NET-Core-Web-API/
│
├── Controllers/
│ └── AuthController.cs
│ └── MyRequestController.cs
│
├── Models/
│ └── LoginInfo.cs
│
├── Service/
│ └── JwtService.cs
│
├── Program.cs
├── appsettings.json
└── README.md
- User sends credentials to
/api/Auth/Login. - If valid, a JWT token is returned.
- Token is passed in the
Authorizationheader asBearer <token>. - Secure endpoints require
[Authorize]and validate the token.
- Run the project.
- Open Swagger UI at
/swagger. - Use the Authorize button to enter your JWT token:
- Call secure endpoints like
/api//MyRequest/GetMySecureData.
Update appsettings.json with your JWT settings:Ideally read this from Azure key vault
"Jwt": {
"Key": "My_Super_Authentication_Super_Secreatkey",
"Issuer": "myjwt_issuer",
"Audience": "myjwt_audience",
"Expires": 5
}The project includes a dedicated service that generates JWTs:
- Signs token with HMAC-SHA256
- Adds standard claims (sub, jti)
- Supports configurable expiration time
🙌 Author
Prashant
Authentication | Jwt token | .NET | API Security | Backend Development