Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. CWE-400: Implementing Rate Limiting in ASP.NET Core to Prevent Denial of Service

CWE-400: Implementing Rate Limiting in ASP.NET Core to Prevent Denial of Service

Date- Jun 07,2026 329
cwe 400 rate limiting

Overview

Rate limiting is a crucial technique in application design that restricts the number of requests a user can make to a server within a specific time frame. This mechanism is essential for preventing Denial of Service (DoS) attacks, where an attacker overwhelms a service with excessive requests, rendering it unavailable to legitimate users. By implementing rate limiting, developers can safeguard their applications from such attacks, ensuring a stable and reliable user experience.

Real-world use cases for rate limiting include APIs exposed to third-party developers, login endpoints that are prime targets for brute-force attacks, and any service that experiences unpredictable traffic spikes. For example, an e-commerce site may implement rate limiting during a flash sale to prevent a surge in requests from crashing the backend service. By effectively managing the flow of requests, applications can maintain performance and availability.

Prerequisites

  • ASP.NET Core knowledge: Familiarity with middleware, dependency injection, and basic application structure.
  • Understanding of HTTP: Basic knowledge of HTTP methods, status codes, and headers is essential.
  • NuGet Package Manager: Required for installing necessary libraries for rate limiting.
  • Development Environment: Visual Studio or any suitable IDE for ASP.NET Core development.

Understanding Rate Limiting Strategies

Rate limiting can be implemented using various strategies, each with its advantages and trade-offs. The most common strategies include fixed window, sliding window, and token bucket. Understanding these strategies is crucial to selecting the right approach for your application.

The fixed window algorithm counts the number of requests in a predefined time frame (e.g., 1 minute). If the limit is exceeded, further requests are denied until the next time window begins. This method is simple but can lead to burst traffic at the boundary of the time window.

public class FixedWindowRateLimiterMiddleware { private readonly RequestDelegate _next; private static readonly Dictionary<string, (int Count, DateTime ResetTime)> _clients = new(); private const int _limit = 100; private const int _timeWindowInSeconds = 60; public FixedWindowRateLimiterMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { var clientId = context.Connection.RemoteIpAddress.ToString(); if (_clients.TryGetValue(clientId, out var clientInfo)) { if (DateTime.UtcNow < clientInfo.ResetTime) { if (clientInfo.Count >= _limit) { context.Response.StatusCode = StatusCodes.Status429TooManyRequests; return; } _clients[clientId] = (clientInfo.Count + 1, clientInfo.ResetTime); } else { _clients[clientId] = (1, DateTime.UtcNow.AddSeconds(_timeWindowInSeconds)); } } else { _clients[clientId] = (1, DateTime.UtcNow.AddSeconds(_timeWindowInSeconds)); } await _next(context); } }

This code snippet defines a middleware that implements the fixed window rate limiting approach. It uses a dictionary to track the request count and the reset time for each client identified by their IP address.

Line-by-line explanation:

  1. RequestDelegate _next: A delegate representing the next middleware in the pipeline.
  2. Dictionary<string, (int Count, DateTime ResetTime)> _clients: Stores the request count and reset time for each client.
  3. public FixedWindowRateLimiterMiddleware(RequestDelegate next): Constructor initializing the middleware.
  4. public async Task InvokeAsync(HttpContext context): The main method that processes each incoming request.
  5. var clientId = context.Connection.RemoteIpAddress.ToString(); Retrieves the client's IP address.
  6. if (_clients.TryGetValue(clientId, out var clientInfo)): Checks if the client exists in the dictionary.
  7. if (DateTime.UtcNow < clientInfo.ResetTime): Checks if the request is within the allowed time frame.
  8. if (clientInfo.Count >= _limit): Denies the request if the limit is exceeded.
  9. _clients[clientId] = (clientInfo.Count + 1, clientInfo.ResetTime); Increments the request count for the client.
  10. else: Resets the count and updates the reset time for the client.
  11. await _next(context); Invokes the next middleware in the pipeline.

Expected output: If a client exceeds the limit of 100 requests in one minute, they will receive a 429 Too Many Requests response.

Sliding Window Rate Limiting

Sliding window rate limiting offers a more granular approach by allowing requests to be counted within a sliding time frame, rather than a fixed one. This strategy is more forgiving, allowing bursts of traffic while still enforcing a limit over time.

public class SlidingWindowRateLimiterMiddleware { private readonly RequestDelegate _next; private static readonly Dictionary<string, List<DateTime>> _clients = new(); private const int _limit = 100; private const int _timeWindowInSeconds = 60; public SlidingWindowRateLimiterMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { var clientId = context.Connection.RemoteIpAddress.ToString(); if (!_clients.ContainsKey(clientId)) { _clients[clientId] = new List<DateTime>(); } var currentTime = DateTime.UtcNow; _clients[clientId].RemoveAll(timestamp => (currentTime - timestamp).TotalSeconds > _timeWindowInSeconds); if (_clients[clientId].Count >= _limit) { context.Response.StatusCode = StatusCodes.Status429TooManyRequests; return; } _clients[clientId].Add(currentTime); await _next(context); } }

This middleware uses a list to track timestamps of requests for each client. It removes timestamps that are older than the defined time window, allowing for more accurate counting of requests.

Line-by-line explanation:

  1. Dictionary<string, List<DateTime>> _clients: Stores the timestamps of requests for each client.
  2. if (!_clients.ContainsKey(clientId)): Initializes the client's list if they are new.
  3. RemoveAll: Cleans up timestamps older than the time window.
  4. if (_clients[clientId].Count >= _limit): Denies the request if the limit is reached.
  5. _clients[clientId].Add(currentTime); Adds the current timestamp to the client's list.

Expected output: Similar to the fixed window, clients will receive a 429 response if they exceed 100 requests in a 60-second window.

Token Bucket Rate Limiting

The token bucket algorithm allows a certain number of requests to be processed concurrently, with tokens being added to the bucket over time. Each request consumes a token; if the bucket is empty, requests are denied until tokens are replenished. This method is particularly useful for APIs that require burstable traffic while maintaining an average rate.

public class TokenBucketRateLimiterMiddleware { private readonly RequestDelegate _next; private static readonly Dictionary<string, (int Tokens, DateTime LastRefill)> _clients = new(); private const int _capacity = 100; private const int _refillRate = 1; private const int _refillIntervalInSeconds = 1; public TokenBucketRateLimiterMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { var clientId = context.Connection.RemoteIpAddress.ToString(); if (!_clients.ContainsKey(clientId)) { _clients[clientId] = (_capacity, DateTime.UtcNow); } var (tokens, lastRefill) = _clients[clientId]; var currentTime = DateTime.UtcNow; var timeSinceLastRefill = (currentTime - lastRefill).TotalSeconds; var tokensToAdd = (int)(timeSinceLastRefill / _refillIntervalInSeconds); tokens = Math.Min(_capacity, tokens + tokensToAdd); if (tokens > 0) { _clients[clientId] = (tokens - 1, currentTime); await _next(context); } else { context.Response.StatusCode = StatusCodes.Status429TooManyRequests; } } }

This implementation of the token bucket algorithm maintains a count of tokens available for each client, allowing for more flexible rate limiting.

Line-by-line explanation:

  1. Dictionary<string, (int Tokens, DateTime LastRefill)> _clients: Stores the token count and last refill time for each client.
  2. if (!_clients.ContainsKey(clientId)): Initializes the client's tokens if they are new.
  3. var tokensToAdd = (int)(timeSinceLastRefill / _refillIntervalInSeconds): Calculates how many tokens to add based on the time elapsed since the last refill.
  4. tokens = Math.Min(_capacity, tokens + tokensToAdd): Ensures the token count does not exceed the capacity.
  5. if (tokens > 0): Processes the request if a token is available.
  6. context.Response.StatusCode = StatusCodes.Status429TooManyRequests: Denies the request if no tokens are available.

Expected output: Clients receive a 429 response if they attempt to make requests when no tokens are left in the bucket.

Edge Cases & Gotchas

While implementing rate limiting, developers may encounter several edge cases and pitfalls. One common issue is the potential for IP spoofing, where an attacker uses multiple IP addresses to circumvent rate limits. To mitigate this, consider using additional identifying factors such as API keys or user accounts.

// Incorrect approach: Relying solely on IP address for rate limiting

In this example, relying only on the IP address may lead to vulnerabilities. A better approach is to combine IP address checks with user authentication.

// Correct approach: Combining IP and user authentication for rate limiting

Using both the IP address and user authentication helps to create a more robust rate limiting strategy.

Performance & Best Practices

When implementing rate limiting, it's essential to consider the performance impact of the chosen strategy. For instance, the fixed window approach may perform better under low traffic but can introduce latency during high traffic due to the need for counting requests.

Best practices for optimizing rate limiting include:

  • Use in-memory caching: For lightweight applications, consider using in-memory caches like MemoryCache for storing rate limit data.
  • Distributed caching: For larger applications, utilize distributed caches like Redis to share rate limit data across instances.
  • Asynchronous processing: Ensure that rate limiting logic is non-blocking to avoid degrading performance.
  • Monitoring and logging: Implement logging to monitor rate limit events and adjust thresholds based on usage patterns.

Real-World Scenario: Building a Rate-Limited API

To showcase the implementation of rate limiting, let's create a simple API that allows users to fetch data but limits the number of requests they can make. We will use the token bucket approach for flexibility.

public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSingleton<TokenBucketRateLimiterMiddleware>(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseMiddleware<TokenBucketRateLimiterMiddleware>(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } public class DataController : ControllerBase { [HttpGet("/data")] public IActionResult GetData() { return Ok(new { Message = "Here is your data!" }); } }

In this example, we define a simple API with a single endpoint that returns data. The rate limiting middleware is registered in the pipeline, ensuring that all requests to this endpoint are subject to the defined limits.

Line-by-line explanation:

  1. public void ConfigureServices(IServiceCollection services): Configures services, including the middleware.
  2. app.UseMiddleware<TokenBucketRateLimiterMiddleware>(); Adds the rate limiting middleware to the request pipeline.
  3. endpoints.MapControllers(); Maps the API controllers to endpoints.

Conclusion

  • Rate limiting is essential for protecting applications against DoS attacks.
  • Understanding different strategies like fixed window, sliding window, and token bucket is crucial for effective implementation.
  • Edge cases such as IP spoofing should be considered to strengthen rate limiting.
  • Performance optimizations include using caching and asynchronous processing.
  • Real-world implementations can leverage these strategies to create resilient APIs.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

CWE-770: Configuring Resource Limits and Request Throttling in ASP.NET Core
Jun 08, 2026
Handling Wrong Content-Type Header in ASP.NET Core API
Apr 22, 2026
Understanding 401 Unauthorized in ASP.NET Core: The Importance of UseAuthentication()
Apr 22, 2026
Implementing IP Whitelisting and Blacklisting Middleware in ASP.NET Core
Jun 10, 2026
Previous in ASP.NET Core
CWE-778: Implementing Security Audit Logging in ASP.NET Core with…
Next in ASP.NET Core
CWE-770: Configuring Resource Limits and Request Throttling in AS…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,205 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,937 views
  • 4
    Error-An error occurred while processing your request in .… 11,964 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 243 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 828 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 613 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26683 views
  • Exception Handling Asp.Net Core 21720 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21177 views
  • How to implement Paypal in Asp.Net Core 20127 views
  • Task Scheduler in Asp.Net core 18201 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor