RejectionStatusCode (#22)

pull/23/head^2
Rick Anderson 2022-08-23 17:55:21 -10:00 committed by GitHub
parent 87b52884d1
commit bae4c1836f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 28 deletions

View File

@ -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
// <snippet_fixed>
@ -396,7 +396,10 @@ var jwtPolicyName = "jwt";
var myOptions = new MyRateLimitOptions();
app.Configuration.GetSection(MyRateLimitOptions.MyRateLimit).Bind(myOptions);
app.UseRateLimiter(new RateLimiterOptions()
var options = new RateLimiterOptions()
{
RejectionStatusCode = StatusCodes.Status429TooManyRequests
}
.AddPolicy(policyName: jwtPolicyName, partitioner: httpContext =>
{
var accessToken = httpContext?.Features?.Get<IAuthenticateResultFeature>()?
@ -404,7 +407,7 @@ app.UseRateLimiter(new RateLimiterOptions()
?? string.Empty;
if (!StringValues.IsNullOrEmpty(accessToken))
{
return RateLimitPartition.CreateTokenBucketLimiter( accessToken, key =>
return RateLimitPartition.CreateTokenBucketLimiter(accessToken, key =>
new TokenBucketRateLimiterOptions(tokenLimit: myOptions.tokenLimit2,
queueProcessingOrder: QueueProcessingOrder.OldestFirst,
queueLimit: myOptions.queueLimit,
@ -422,7 +425,9 @@ app.UseRateLimiter(new RateLimiterOptions()
tokensPerPeriod: myOptions.tokensPerPeriod,
autoReplenishment: true));
}
}));
});
app.UseRateLimiter(options);
app.MapGet("/", () => "Hello, World!");