Skip to content

timleunghk/JwtAuthDemo

Repository files navigation

🚀 JWT Auth Demo API

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.

📌 Features

  • ✅ 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)

🗂 Project Structure

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

⚡ Getting Started

  1. Clone the Repository

    git clone https://github.com/your-username/jwt-auth-demo.git
    cd jwt-auth-demo
  2. Install Dependencies

    dotnet restore
  3. Run the API

    dotnet run

    By default, it listens on:

🔑 Authentication Flow

  1. Login → Get JWT Token

    POST /api/auth/login
    Content-Type: application/json
    
    {
      "username": "test",
      "password": "password"
    }
    

    Response:

    {
      "token": "<your-jwt-here>"
    }
  2. Authorize in Swagger

    • Go to Swagger UI (/swagger)
    • Click "Authorize"
    • Paste only the token (Swagger auto-prepends Bearer)
  3. Example Protected Endpoint

    GET /api/secure/profile
    Authorization: Bearer <your-jwt-here>
    

    Response:

    {
      "username": "test",
      "message": "This is a protected endpoint"
    }

Demo Images

  1. Login → Get JWT Token

    Login Demo

  2. Access Protected Endpoint

    Secure Endpoint Demo

⚙️ Configuration

JWT settings are inside appsettings.json:

"Jwt": {
  "Key": "ThisIsYourSecretKeyForJwtDontUseInProduction123!",
  "Issuer": "JwtAuthDemo",
  "Audience": "JwtAuthDemoClient",
  "ExpireMinutes": 60
}

👉 Update Key to a stronger secret for production!

🧰 Tech Stack

📖 Swagger Docs

Once the API is running, navigate to:

You’ll see all endpoints documented and can try them directly in the browser.

🤝 Contributing

Pull requests, bug reports, and feature requests are welcome!

  1. Fork the repo
  2. Create a new branch (feature/your-feature)
  3. Commit your changes
  4. Submit a PR 🚀

📜 License

This demo project is licensed under the MIT License.
Use it freely for learning, demos, or as a project starter!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages