Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nimanikoo/Dotnet-RateLimiter/llms.txt
Use this file to discover all available pages before exploring further.
RedisRateLimitingMiddleware sits in the ASP.NET Core request pipeline and acts as the bridge between the routing layer and RedisRateLimiter. For every incoming request it checks whether the matched endpoint is decorated with [RedisRateLimit], identifies the calling client (authenticated user or anonymous guest), constructs a composite Redis key, and either lets the request proceed or short-circuits it with an HTTP 429 Too Many Requests response before any controller code runs.
Namespace
Constructor
app.UseMiddleware<RedisRateLimitingMiddleware>() is called.
The next middleware in the pipeline. Invoked when the current request is within the
rate limit quota and should continue processing.
The singleton
RedisRateLimiter service that executes the atomic Lua counter check
against Redis. Injected from the DI container.Method: InvokeAsync
Execution Flow
Resolve the current endpoint
Calls
context.GetEndpoint(). If no endpoint is matched (e.g. a 404 path), the
middleware immediately forwards to _next(context) without any rate-limit check.Check for RedisRateLimitAttribute
Looks up
RedisRateLimitAttribute in the endpoint’s metadata collection. If the
attribute is absent, the endpoint is not rate-limited and the request passes through.Resolve client identity
Determines whether the caller is authenticated or anonymous and sets both
identityKey and userType accordingly:- Authenticated — reads
ClaimTypes.NameIdentifierfrom the user’s claims; falls back toIdentity.Name, then to the string"authenticated_unknown". SetsuserType = "user". - Anonymous — reads
context.Connection.RemoteIpAddress; falls back to"unknown". SetsuserType = "guest".
Build the rate-limit key
Combines
userType, identityKey, and the request path into a single composite
string. RedisRateLimiter will further prefix this with rate_limit: before writing
it to Redis.Evaluate the rate limit in Redis
Delegates to
RedisRateLimiter.IsAllowedAsync, passing the composite key, the
MaxRequests and the WindowSeconds (converted to a TimeSpan) from the attribute.Full InvokeAsync Implementation
HTTP 429 Response Details
When a request exceeds the configured limit the middleware writes the following response and does not call_next, so no controller code executes:
| Property | Value |
|---|---|
| Status code | 429 Too Many Requests |
Retry-After header | {WindowSeconds} (the raw integer from the attribute) |
| Body — authenticated user | Dear user, you've reached your limit. Take a breath! |
| Body — anonymous guest | Guest limit reached. Please sign up for more quota! |
Registration
Register the middleware inProgram.cs after UseAuthorization so that authentication
claims are already populated when InvokeAsync reads context.User:
UseMiddleware<RedisRateLimitingMiddleware>() must be placed after
app.UseRouting() / endpoint routing is set up (which MapControllers implies) and
after app.UseAuthorization(). Placing it before authentication or routing will
cause the endpoint and user identity lookups to return null for every request.