From bae4c1836f9f0520ddad29f8364792cbb685d5b6 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 23 Aug 2022 17:55:21 -1000 Subject: [PATCH] RejectionStatusCode (#22) --- .../rate-limit/WebRateLimitAuth/Program.cs | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs b/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs index 015c07b..faf606a 100644 --- a/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs +++ b/fundamentals/middleware/rate-limit/WebRateLimitAuth/Program.cs @@ -1,4 +1,4 @@ -#define FIRST // FIRST ADMIN FIXED SLIDING CONCUR TOKEN FIXED2 JWT +#define JWT // FIRST ADMIN FIXED SLIDING CONCUR TOKEN FIXED2 JWT #if NEVER #elif FIXED // @@ -396,33 +396,38 @@ var jwtPolicyName = "jwt"; var myOptions = new MyRateLimitOptions(); app.Configuration.GetSection(MyRateLimitOptions.MyRateLimit).Bind(myOptions); -app.UseRateLimiter(new RateLimiterOptions() - .AddPolicy(policyName: jwtPolicyName, partitioner: httpContext => - { - var accessToken = httpContext?.Features?.Get()? - .AuthenticateResult?.Properties?.GetTokenValue("access_token")?.ToString() - ?? string.Empty; - if (!StringValues.IsNullOrEmpty(accessToken)) - { - return RateLimitPartition.CreateTokenBucketLimiter( accessToken, key => - new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit2, - queueProcessingOrder: QueueProcessingOrder.OldestFirst, - queueLimit: myOptions.queueLimit, - replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), - tokensPerPeriod: myOptions.tokensPerPeriod, - autoReplenishment: myOptions.autoReplenishment)); - } - else - { - return RateLimitPartition.CreateTokenBucketLimiter("Anon", key => - new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit, - queueProcessingOrder: QueueProcessingOrder.OldestFirst, - queueLimit: myOptions.queueLimit, - replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), - tokensPerPeriod: myOptions.tokensPerPeriod, - autoReplenishment: true)); - } - })); +var options = new RateLimiterOptions() +{ + RejectionStatusCode = StatusCodes.Status429TooManyRequests +} + .AddPolicy(policyName: jwtPolicyName, partitioner: httpContext => + { + var accessToken = httpContext?.Features?.Get()? + .AuthenticateResult?.Properties?.GetTokenValue("access_token")?.ToString() + ?? string.Empty; + if (!StringValues.IsNullOrEmpty(accessToken)) + { + return RateLimitPartition.CreateTokenBucketLimiter(accessToken, key => + new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit2, + queueProcessingOrder: QueueProcessingOrder.OldestFirst, + queueLimit: myOptions.queueLimit, + replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), + tokensPerPeriod: myOptions.tokensPerPeriod, + autoReplenishment: myOptions.autoReplenishment)); + } + else + { + return RateLimitPartition.CreateTokenBucketLimiter("Anon", key => + new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit, + queueProcessingOrder: QueueProcessingOrder.OldestFirst, + queueLimit: myOptions.queueLimit, + replenishmentPeriod: TimeSpan.FromSeconds(myOptions.replenishmentPeriod), + tokensPerPeriod: myOptions.tokensPerPeriod, + autoReplenishment: true)); + } + }); + +app.UseRateLimiter(options); app.MapGet("/", () => "Hello, World!");