A lightweight, open-source ASP.NET Core application demonstrating JWT (JSON Web Token) authentication implementation.
It’s a companion to my post, where I dive deeper.
JWT Auth Demo is a simple yet comprehensive example of how to implement JWT-based authentication in ASP.NET Core applications. This project serves as both a reference implementation and a starting point for developers looking to add secure authentication to their web applications.
- JWT token generation and validation
- User authentication with in-memory database
- Secure password handling
- Protected API endpoints
- Clean, easy-to-understand architecture
- ASP.NET Core (.NET 9.0)
- Entity Framework Core with In-Memory Database
- JWT Bearer Authentication
- .NET 9.0 SDK or later
- Any code editor (Visual Studio, VS Code, JetBrains Rider, etc.)
-
Clone the repository
git clone https://github.com/yourusername/jwt-auth-demo.git cd jwt-auth-demo
-
Build the application
dotnet build
-
Run the application
dotnet run
The application will start on https://localhost:5001
by default.
To authenticate and receive a JWT token:
POST /api/auth/login
Content-Type: application/json
{
"username": "[email protected]",
"password": "yourpassword"
}
Include the token in the Authorization header for subsequent requests:
GET /api/protected-resource
Authorization: Bearer your_jwt_token_here
Controllers/
- API endpointsData/
- Database context and modelsServices/
- Business logic including authentication service
JWT settings can be configured in the appsettings.json
file:
{
"Jwt": {
"Key": "your-secret-key-here",
"Issuer": "your-issuer",
"Audience": "your-audience",
"ExpiryInMinutes": 60
}
}
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- ASP.NET Core team for the excellent authentication framework
- The open-source community for inspiration and guidance