Introduction
Nowadays, many companies are moving from monolithic applications to microservices because microservices are scalable, easier to maintain, and allow each service to be deployed independently.
However, adopting microservices introduces new challenges. One of the most critical challenges is security.
In monolithic applications, security is simpler because there is a single application with one entry point. In microservices, multiple services expose APIs and communicate with each other over the network, making security more complex.
This article explains how to implement security in a microservices architecture using:
JWT (JSON Web Tokens)
API Gateway
YARP (Yet Another Reverse Proxy) in ASP.NET Core
Why Security Is More Complex in Microservices
In monolithic systems, there is typically:
One application
One entry point
One authentication middleware
One deployment
In microservices, however:
Many services run independently
Each service exposes its own API endpoints
Services communicate over the network
Services scale dynamically
IP addresses and ports change frequently
As a result, the system has multiple entry points, making security harder to manage.
Main Goals of Microservices Security
The primary goals of securing microservices are:
Correctly authenticate users
Authorize users based on roles or permissions
Ensure only trusted systems can call internal services
Prevent direct access to microservices
Secure service-to-service communication
Protect tokens and sensitive data
What Is JWT and How It Works
A JSON Web Token (JWT) is a secure method for transmitting information between a client and a server. It is commonly used in APIs to authenticate users and prevent unauthorized access.
A JWT consists of JSON data that is digitally signed, ensuring the token cannot be altered.
JWT Structure
A JWT has three parts:
1. Header
Contains metadata about the token, such as:
Token type (JWT)
Signing algorithm (HMAC, RSA, ECDSA, etc.)
2. Payload
Contains claims (user-related information), such as:
Username
Role
User ID
3. Signature
Ensures token authenticity by verifying:
The token was issued by a trusted authority
The payload has not been modified
JWT Format
xxxxx.yyyyy.zzzzz
Where:
xxxxx– Headeryyyyy– Payloadzzzzz– Signature
Why JWT Is Used in Microservices
JWT is well suited for microservices because it:
Is stateless (no server-side sessions)
Includes authentication data in each request
Works well in distributed environments
Supports claims and roles
Is easy to validate
Where Authentication Should Happen
There are two common approaches to authentication in microservices.
Approach 1: Each Microservice Handles Authentication
In this approach:
Every service validates the JWT token
Every service enforces role and permission checks
Pros
Independent security per service
Works even if the gateway is removed
Cons
Duplicate security logic across services
Harder to manage policies
Increased configuration and development effort
Approach 2: API Gateway Handles Authentication (Recommended)
In this approach:
The client sends the JWT token to the API Gateway
The gateway validates the token
Requests are forwarded to internal services
Microservices remain private
Pros
Centralized security configuration
Cleaner microservices
Easier implementation of cross-cutting concerns
Cons
Gateway becomes a critical component
Internal traffic still requires protection
Recommended Architecture: JWT + API Gateway
The request flow is:
Client authenticates with AuthService and receives a JWT
Client sends requests to the API Gateway with the JWT
Gateway validates the token
Gateway routes the request to the target microservice
Response flows back through the gateway
Implementation Overview
The example uses three projects:
AuthService – Generates JWT tokens
ApiGateway – Uses YARP for routing and JWT validation
OrderService – Internal service protected behind the gateway
Step 1: AuthService (JWT Generation)
Responsibilities
Validate user credentials
Generate JWT tokens
Return tokens to clients
Required Package
dotnet add package System.IdentityModel.Tokens.Jwt
appsettings.json
{
"Jwt": {
"Key": "THIS_IS_A_DEMO_SECRET_KEY_CHANGE_IT",
"Issuer": "MicroservicesAuthServer",
"Audience": "MicroservicesClients",
"ExpireMinutes": 60
}
}

Join the conversation! Your thoughts help the community grow.