As organizations adopt microservices architectures, securing communication between clients and distributed services becomes a critical challenge. Instead of exposing every microservice directly to the outside world, an API gateway acts as a single entry point that provides security, routing, load balancing, and observability.

This article explores common security patterns for API gateways in ASP.NET Core microservices and how to implement them effectively.

1. Why Use an API Gateway?

Popular ASP.NET Core options:

2. Security Patterns for API Gateways

2.1. Authentication at the Gateway

Centralize authentication so individual microservices don’t have to implement it.

Example with Ocelot and JWT Authentication:

builder.Services.AddAuthentication("Bearer")
    .AddJwtBearer("Bearer", options =>
    {
        options.Authority = "https://identity.example.com";
        options.TokenValidationParameters = new()
        {
            ValidateAudience = false
        };
    });

builder.Services.AddOcelot();

Ocelot configuration (ocelot.json):

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/orders",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [{ "Host": "orderservice", "Port": 5001 }],
      "UpstreamPathTemplate": "/orders",
      "AuthenticationOptions": { "AuthenticationProviderKey": "Bearer" }
    }
  ]
}

2.2. Authorization Delegation

2.3. Rate Limiting & Throttling

Prevent abuse and DoS attacks by limiting requests per client.

Ocelot rate limiting example:

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/api/products",
      "DownstreamPathTemplate": "/api/products",
      "RateLimitOptions": {
        "ClientWhitelist": [ "dev-client" ],
        "EnableRateLimiting": true,
        "Period": "1s",
        "PeriodTimespan": 1,
        "Limit": 5
      }
    }
  ]
}

This allows only 5 requests per second per client.

2.4. Input Validation and Request Filtering

The gateway can sanitize input before requests reach microservices.

2.5. Centralized Logging & Monitoring

The API Gateway is the best place to implement:

app.Use(async (context, next) =>
{
    var correlationId = Guid.NewGuid().ToString();
    context.Response.Headers.Add("X-Correlation-ID", correlationId);
    await next();
});

2.6. TLS Termination and HTTPS Enforcement

app.UseHsts();
app.UseHttpsRedirection();

2.7. Caching and Response Shaping

2.8. API Key Management (Optional)

For external APIs, issue API keys and validate them at the gateway.

3. API Gateway Security Architecture

A secure ASP.NET Core microservices setup typically looks like this:

[ Client Apps ] 
       |
       v
 [ API Gateway ]  --->  Authentication / Authorization
       |               --->  Rate Limiting / Input Validation
       v
 [ Internal Microservices ]

4. Best Practices Checklist

Conclusion

An API gateway is more than just a reverse proxy; it’s the security guard of your microservices architecture. By centralizing authentication, authorization, rate limiting, and monitoring, you significantly reduce the attack surface and improve resilience. ASP.NET Core developers can use tools like Ocelot or YARP to implement these patterns, while cloud-native solutions like Azure API Management provide enterprise-grade features out of the box.