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,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<IAuthenticateResultFeature>()?
.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<IAuthenticateResultFeature>()?
.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!");