AspNetCoreRateLimit icon indicating copy to clipboard operation
AspNetCoreRateLimit copied to clipboard

Customizing Response When Too Many Request

Open msadeqsirjani opened this issue 3 years ago • 1 comments

Hi I have question How Can I customize response when rate limiter raised?

msadeqsirjani avatar Jan 20 '22 19:01 msadeqsirjani

Create your own rate limit middleware which inherits from one of the existing middleware implementations, and override ReturnQuotaExceededResponse.

Eg.

public override Task ReturnQuotaExceededResponse(HttpContext context, RateLimitRule rule, string retryAfter)
{
    context.Response.Headers["Retry-After"] = retryAfter;
    context.Response.StatusCode = _options.QuotaExceededResponse?.StatusCode ?? _options.HttpStatusCode;

    // ...

    return Task.CompletedTask;
}

jacobmstein avatar Jan 26 '22 20:01 jacobmstein