This project implements two REST APIs in C# using ASP.NET Core (.NET 8) for learning about JWT authentication and inter-service communication. API 1 is a protected service that requires a valid JWT token, while API 2 consumes API 1 by generating a JWT token and returning the protected data.
- Demonstrate how to configure JWT authentication in an API
- Show how one API can consume another using JWT tokens
- Teach basic concepts of inter-API communication with C#
- Endpoint:
GET /protected - Function: Returns a JSON message (
{"Message": "Access granted! Protected data here."}) if a valid JWT token is provided in theAuthorization: Bearer <token>header - Technologies:
- ASP.NET Core with
Microsoft.AspNetCore.Authentication.JwtBearerfor token validation System.IdentityModel.Tokens.Jwtfor JWT handling
- ASP.NET Core with
- Endpoint:
GET /consume - Function: Generates a JWT token, makes a request to API 1, and returns the received data or an error
- Technologies:
System.IdentityModel.Tokens.Jwtfor token generationHttpClientfor communication with API 1
- .NET SDK (version 8 or higher)
- Tools for API testing (e.g.,
curl, Postman, or browser) - NuGet packages:
- API 1:
Microsoft.AspNetCore.Authentication.JwtBearer,System.IdentityModel.Tokens.Jwt - API 2:
System.IdentityModel.Tokens.Jwt
- API 1:
-
Create projects:
dotnet new webapi -n Api1 cd Api1 dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer dotnet add package System.IdentityModel.Tokens.Jwt cd .. dotnet new webapi -n Api2 cd Api2 dotnet add package System.IdentityModel.Tokens.Jwt
-
Configure API 1:
- Replace
Api1/Program.cswith the provided code (contains JWT authentication configuration) - Use secret key:
minha_chave_secreta_super_segura_1234567890(minimum 32 characters for HS256)
- Replace
-
Configure API 2:
- Replace
Api2/Program.cswith the provided code (contains JWT generation and API 1 call) - Use the same secret key as API 1
- Replace
-
Start API 1:
cd Api1 dotnet run --urls=http://localhost:5000 -
Start API 2:
cd Api2 dotnet run --urls=http://localhost:5001 -
Test API 2:
curl http://localhost:5001/consume
- Expected output:
{"DataFromApi1":{"Message":"Access granted! Protected data here."}}
- Expected output:
-
Test API 1 directly (optional):
curl http://localhost:5000/protected(without token) → 401 Unauthorized error- Use Postman with a token generated by API 2 to test
- Configures JWT authentication with symmetric key validation (HS256)
- Protects the
/protectedendpoint with[Authorize] - Uses 256-bit or larger secret key
- Generates a JWT with
user_idclaim and 15-minute expiration - Uses
HttpClientto call API 1 with the token in theAuthorizationheader - Returns API 1 data or error
-
JWT (JSON Web Token):
- Token generation with claims and expiration
- Token validation with symmetric keys
- Using
Authorization: Bearer <token>header
-
ASP.NET Core:
- Authentication configuration with
JwtBearer - Creating REST APIs with minimal APIs
- Dependency injection (
IHttpClientFactory)
- Authentication configuration with
-
Inter-API Communication:
- Sending HTTP requests with
HttpClient - Handling responses (success and error)
- Sending HTTP requests with
-
"key size must be greater than 256 bits" error:
- Use a secret key with at least 32 characters (e.g.,
minha_chave_secreta_super_segura_1234567890) - Confirm both APIs use the same key
- Use a secret key with at least 32 characters (e.g.,
-
401 error on /protected:
- Check if the token is in the correct format (
Bearer <token>) - Test with Postman for debugging
- Check if the token is in the correct format (
-
API not responding:
- Confirm ports 5000 (API 1) and 5001 (API 2) are available
- Verify projects are running correctly (
dotnet run)