Denial of Service (DoS) and Distributed Denial of Service (DDoS) attacks aim to make your application or API unavailable by overwhelming it with excessive requests, consuming resources like CPU, memory, bandwidth, or database connections. While complete prevention often requires infrastructure-level protection (firewalls, CDNs, reverse proxies), ASP.NET Core applications can still implement multiple layers of defense to mitigate the risks.

1. Understanding DoS/DDoS in Web Applications

Common symptoms

2. Mitigation Strategies in ASP.NET Core

2.1. Use Reverse Proxies, Firewalls, and CDNs

2.2. Enable Rate Limiting and Throttling

ASP.NET Core 7+ has built-in Rate Limiting Middleware.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRateLimiter(options =>
{
    options.AddFixedWindowLimiter("fixed", c =>
    {
        c.PermitLimit = 100; // Max requests
        c.Window = TimeSpan.FromSeconds(10); // Per 10 seconds
        c.QueueLimit = 2;
        c.QueueProcessingOrder = System.Threading.RateLimiting.QueueProcessingOrder.OldestFirst;
    });
});

var app = builder.Build();

app.UseRateLimiter();

app.MapGet("/api/data", () => "Rate limited data")
   .RequireRateLimiting("fixed");

app.Run();

This prevents abusive clients from flooding endpoints.

2.3. Limit Request Body Size

Attackers may send extremely large payloads to consume server memory.

builder.Services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = 10 * 1024; // 10 KB
});

builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = 10 * 1024; // 10 KB
});

2.4. Validate Input Early

Malicious requests with deeply nested JSON, large files, or invalid formats can exhaust processing power.

2.5. Protect File Upload Endpoints

File uploads are a common DoS vector.

if (file.Length > 5 * 1024 * 1024)
{
    return Results.BadRequest("File too large!");
}

2.6. Implement Caching

builder.Services.AddMemoryCache();

2.7. Asynchronous Programming

ASP.NET Core’s async model helps reduce thread pool starvation under load. Always use async/await with database and I/O operations.

2.8. Circuit Breakers and Timeouts

Prevent back-end systems from being overloaded by cascading failures:

builder.Services.AddHttpClient("SafeClient")
    .AddPolicyHandler(Polly.Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(2)));

2.9. Connection Limits

Restrict simultaneous connections per IP to reduce resource exhaustion.
On Kestrel:

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxConcurrentConnections = 100;
    options.Limits.MaxConcurrentUpgradedConnections = 10;
});

2.10. Monitoring and Logging

3. Infrastructure-Level Recommendations

ASP.NET Core alone cannot stop massive DDoS attacks. Combine app-level defenses with:

4. Best Practices Checklist

Conclusion

DoS/DDoS attacks are inevitable, but ASP.NET Core provides powerful features to limit, throttle, and validate requests, while infrastructure tools like WAFs and CDNs provide large-scale protection. By combining app-level safeguards with infrastructure-level defense, you can ensure that your ASP.NET Core applications remain resilient, performant, and available even under hostile traffic conditions.