A .NET Web API project demonstrating JWT Authentication, Authorization, and Swagger/OpenAPI documentation.
Perfect as a starter for secure APIs with protected and public endpoints.
- ✅ User login with JWT token generation
- 🔒 Secured endpoints using Bearer tokens
- 🛠 Built with .NET 9 + C#
- 📖 Integrated Swagger UI with JWT support
- 🎯 Clean and minimal setup (great for demos or extensions)
Controllers/
│ ├── AuthController.cs # Handles /api/auth/login
│ └── SecureController.cs # Example protected endpoint
Program.cs # App startup, middleware, Swagger, JWT config
appsettings.json # JWT config (issuer, audience, secret)
README.md
Models/
│ └── LoginRequest.cs # Login request model
Services/
│ └── JwtService.cs # JWT token generation logic
-
Clone the Repository
git clone https://github.com/your-username/jwt-auth-demo.git cd jwt-auth-demo -
Install Dependencies
dotnet restore
-
Run the API
dotnet run
By default, it listens on:
-
Login → Get JWT Token
POST /api/auth/login Content-Type: application/json { "username": "test", "password": "password" }Response:
{ "token": "<your-jwt-here>" } -
Authorize in Swagger
- Go to Swagger UI (
/swagger) - Click "Authorize"
- Paste only the token (Swagger auto-prepends
Bearer)
- Go to Swagger UI (
-
Example Protected Endpoint
GET /api/secure/profile Authorization: Bearer <your-jwt-here>Response:
{ "username": "test", "message": "This is a protected endpoint" }
JWT settings are inside appsettings.json:
"Jwt": {
"Key": "ThisIsYourSecretKeyForJwtDontUseInProduction123!",
"Issuer": "JwtAuthDemo",
"Audience": "JwtAuthDemoClient",
"ExpireMinutes": 60
}👉 Update Key to a stronger secret for production!
- .NET 9
- C#
- JWT (System.IdentityModel.Tokens.Jwt)
- Swagger (Swashbuckle.AspNetCore)
Once the API is running, navigate to:
You’ll see all endpoints documented and can try them directly in the browser.
Pull requests, bug reports, and feature requests are welcome!
- Fork the repo
- Create a new branch (
feature/your-feature) - Commit your changes
- Submit a PR 🚀
This demo project is licensed under the MIT License.
Use it freely for learning, demos, or as a project starter!

