Wiaoj.RateLimiting.AspNetCore 0.4.0-alpha.5

This is a prerelease version of Wiaoj.RateLimiting.AspNetCore.
dotnet add package Wiaoj.RateLimiting.AspNetCore --version 0.4.0-alpha.5
                    
NuGet\Install-Package Wiaoj.RateLimiting.AspNetCore -Version 0.4.0-alpha.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Wiaoj.RateLimiting.AspNetCore" Version="0.4.0-alpha.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wiaoj.RateLimiting.AspNetCore" Version="0.4.0-alpha.5" />
                    
Directory.Packages.props
<PackageReference Include="Wiaoj.RateLimiting.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Wiaoj.RateLimiting.AspNetCore --version 0.4.0-alpha.5
                    
#r "nuget: Wiaoj.RateLimiting.AspNetCore, 0.4.0-alpha.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Wiaoj.RateLimiting.AspNetCore@0.4.0-alpha.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Wiaoj.RateLimiting.AspNetCore&version=0.4.0-alpha.5&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Wiaoj.RateLimiting.AspNetCore&version=0.4.0-alpha.5&prerelease
                    
Install as a Cake Tool

Wiaoj.RateLimiting.AspNetCore

ASP.NET Core integration package for Wiaoj.RateLimiting, providing HTTP middleware, partition key extractors, endpoint metadata conventions, and RFC-compliant response handling.


Installation

dotnet add package Wiaoj.RateLimiting.AspNetCore

What This Package Contains

  • RateLimitingMiddleware: ASP.NET Core middleware that evaluates incoming requests against configured named or default rate limiting policies, applies endpoint costs, and emits RFC-standard responses.
  • Key Selectors (IRateLimitKeySelector):
    • ClientIpKeySelector: Formats remote IPv4/IPv6 addresses directly using stack-allocated buffers.
    • ApiKeyHeaderKeySelector: Extracts identity keys from configurable request headers (e.g. X-Api-Key), with configurable fallback selectors.
    • UserClaimKeySelector: Extracts identity keys from authenticated claims (ClaimsPrincipal), with configurable fallback selectors.
  • Endpoint Metadata and Route Conventions:
    • [DisableRateLimiting] / .DisableRateLimiting(): Bypasses rate limiting for targeted endpoints.
    • [RateLimitCost(int)] / .WithRateLimitCost(int): Statically overrides the quota cost for an endpoint.
    • .WithRateLimitCost(Func<HttpContext, int>): Dynamically computes quota cost at runtime (e.g. from batch payload counts or query parameters).
  • RFC and Standards Compliance:
    • RFC 6585: Emits HTTP 429 Too Many Requests.
    • RFC 9110: Emits integer delta-seconds Retry-After header.
    • RFC 7807 / RFC 9457: Serializes application/problem+json ProblemDetails responses.
    • IETF Draft Headers: Writes RateLimit-Remaining and RateLimit-Reset headers.

Setup and Configuration

1. Register Services in Program.cs

using Wiaoj.DistributedCounter;
using Wiaoj.RateLimiting;
using Wiaoj.RateLimiting.AspNetCore;
using Wiaoj.RateLimiting.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Configure counter backend
builder.Services.AddDistributedCounter(dc => dc.UseInMemory());

// Configure rate limiting policies and ASP.NET Core options
builder.Services.AddWiaojRateLimiting(limiter => {
    limiter.AddPolicy("auth", policy => policy.UseFixedWindow(limit: 5, window: TimeSpan.FromMinutes(1)));
    limiter.UseDefaultPolicy(policy => policy.UseSlidingWindow(limit: 100, window: TimeSpan.FromMinutes(1)));

    limiter.WithAspNetCore(options => {
        options.KeySelector = new ClientIpKeySelector(prefix: "ip:");
        options.StatusCode = StatusCodes.Status429TooManyRequests;
        options.EnableIetfHeaders = true;
        options.UseProblemDetails = true;
    });
});

var app = builder.Build();

// Enable the middleware in the request pipeline
app.UseWiaojRateLimiting();

Endpoint Routing Conventions

Static Costing, Dynamic Costing, and Exemptions

// 1. Endpoint with static custom cost (consumes 5 quota units per call)
app.MapPost("/api/export", () => Results.Ok())
   .WithRateLimitCost(5);

// 2. Endpoint with dynamic cost calculated from query parameters
app.MapPost("/api/batch", (int batchSize) => Results.Ok())
   .WithRateLimitCost(ctx => {
       if (ctx.Request.Query.TryGetValue("batchSize", out var val) && int.TryParse(val, out int count)) {
           return Math.Max(1, count);
       }
       return 1;
   });

// 3. Endpoint exempt from all rate limiting
app.MapGet("/healthz", () => Results.Ok("OK"))
   .DisableRateLimiting();

Two limits these APIs cannot cross

Both are observable in the shipped code, and both bite as soon as one endpoint serves verbs that do not cost the same.

1. A resolved cost can never be zero. The middleware floors it — cost = Math.Max(1, metadata.DynamicCostResolver(context)) and the same clamp on DefaultCostResolver (Middleware/RateLimitingMiddleware.cs:54 and :63). A resolver that returns 0 to mean "this request is free" is silently recorded as 1. The static path refuses the value even earlier: WithRateLimitCost(int cost) throws ArgumentOutOfRangeException for cost <= 0 (Metadata/RateLimitEndpointConventionBuilderExtensions.cs:18). So "free" is unreachable through either overload — note that example 2 above already defends itself with Math.Max(1, count) rather than returning a raw count; that is the floor, not a style choice.

2. Exemption is endpoint-wide, not per-verb. [DisableRateLimiting] / RateLimitMetadata.IsDisabled short-circuit the whole endpoint (Middleware/RateLimitingMiddleware.cs:45). Minimal APIs let one endpoint answer several methods (app.MapMethods("/items", [HttpMethods.Get, HttpMethods.Query], handler)), and those methods rarely cost the same: a GET on a cacheable read model may be answered from cache semantics and return 304 without producing a body, while QUERY (RFC 10008) parses a payload, validates it against a schema and runs projections. On such a route the only available switches are "count both identically" or "limit neither" — neither expresses "count only the non-GET traffic".

What to do today if you need verb-level accounting: consume Wiaoj.RateLimiting's IRateLimiter directly from an IEndpointFilter that returns early for the methods you want free and calls TryAcquireAsync(policy, key, cost, cancellationToken) for the rest. Policies registered through AddWiaojRateLimiting and the returned RateLimitDecision stay the package's, so only the trigger point lives in your service — and it can move back into endpoint metadata as soon as the verb-level gap closes.

Tracked as wiaoj/libraries#252, which proposes either a Func<HttpContext, bool> ShouldEvaluate on RateLimiterAspNetCoreOptions, or dropping the floors and documenting 0 as "consumes no quota".


Key Selectors

Key selectors define how client identity is extracted from HttpContext:

limiter.WithAspNetCore(options => {
    // 1. Client IP address (IPv4 / IPv6)
    options.KeySelector = new ClientIpKeySelector(prefix: "ip:");

    // 2. API Key header with fallback to IP if missing
    options.KeySelector = new ApiKeyHeaderKeySelector(
        headerName: "X-Api-Key",
        prefix: "api_key:",
        fallbackSelector: new ClientIpKeySelector("anon_ip:"));

    // 3. User claim (Subject ID) with fallback to IP if unauthenticated
    options.KeySelector = new UserClaimKeySelector(
        claimType: ClaimTypes.NameIdentifier,
        prefix: "user:",
        fallbackSelector: new ClientIpKeySelector("anon_ip:"));
});

Response Formatting & ProblemDetails

When a request is rejected, the middleware formats the response according to configured options:

Standard RFC 7807 Response Body (application/problem+json)

{
  "type": "https://tools.ietf.org/html/rfc6585#section-4",
  "title": "Too Many Requests",
  "status": 429,
  "detail": "Rate limit exceeded. Quota will be available in 12 seconds.",
  "instance": "/api/orders",
  "retryAfter": 12,
  "remaining": 0
}

Customizing ProblemDetails

limiter.WithAspNetCore(options => {
    options.UseProblemDetails = true;
    options.ProblemDetailsCustomizer = (problem, context, decision) => {
        problem.Extensions["traceId"] = context.TraceIdentifier;
        problem.Extensions["policy"] = "auth";
    };
});

Custom Low-Level Rejection Callback

limiter.WithAspNetCore(options => {
    options.OnRejectedAsync = (context, decision) => {
        context.Response.StatusCode = StatusCodes.Status429TooManyRequests;
        return context.Response.WriteAsync("Custom plain-text rate limit rejection.");
    };
});

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0-alpha.5 0 11/27/2026
0.4.0-alpha.4 65 11/15/2026
0.4.0-alpha.3 59 11/4/2026
0.4.0-alpha.1 72 10/30/2026
0.3.0-alpha.1 87 10/5/2026
0.2.0-alpha.3 83 9/24/2026
0.2.0-alpha.2 75 9/24/2026
0.2.0-alpha.1 79 9/24/2026
0.1.0-alpha.9 80 9/21/2026
0.1.0-alpha.8 69 9/21/2026
0.1.0-alpha.7 83 9/18/2026
0.1.0-alpha.6 66 9/16/2026
0.1.0-alpha.5 75 9/16/2026
0.1.0-alpha.4 80 9/16/2026
0.1.0-alpha.3 70 9/15/2026
0.1.0-alpha.2 73 9/15/2026
0.1.0-alpha.1 72 9/14/2026
0.0.1-alpha.112-preview 84 9/13/2026
0.0.1-alpha.111-preview 72 9/13/2026
0.0.1-alpha.110-preview 73 9/12/2026
Loading failed