1️⃣ Create Solution Structure
We’ll create 3 projects:
-
AuthService → issues JWT tokens.
-
ProductService → sample microservice.
-
ApiGateway → Ocelot API Gateway.
2️⃣ Implement AuthService (JWT Token Issuer)
Install NuGet packages
Add Token Generation (AuthController.cs)
3️⃣ Implement ProductService (Protected Microservice)
Add a Controller (ProductsController.cs)
Configure JWT Authentication in Program.cs
4️⃣ Configure API Gateway (Ocelot)
Install Ocelot
Add ocelot.json
Configure Program.cs
in ApiGateway
5️⃣ Test Flow
-
Get Token
Response →
{ "token": "eyJhbGci..." }
-
Call Product API via Gateway
✅ Response →
[ { "Id": 1, "Name": "T-shirt", "Price": 499 }, ... ]
-
Without Token → 401 Unauthorized.
🚀 Summary
-
AuthService issues JWT.
-
ProductService validates JWT.
-
ApiGateway (Ocelot) sits in front, validates tokens, and routes traffic.
No comments:
Post a Comment