|
1 |
| -#define TOKEN // FIRST ADMIN FIXED SLIDING CONCUR TOKEN FIXED2 JWT |
| 1 | +#define FIRST // FIRST ADMIN FIXED SLIDING CONCUR TOKEN FIXED2 JWT |
2 | 2 | #if NEVER
|
3 | 3 | #elif FIXED
|
4 | 4 | // <snippet_fixed>
|
|
189 | 189 | app.UseRouting();
|
190 | 190 |
|
191 | 191 | // <snippet>
|
| 192 | +// Preceding code removed for brevity. |
| 193 | + |
192 | 194 | app.UseAuthentication();
|
193 | 195 | app.UseAuthorization();
|
194 | 196 |
|
|
198 | 200 | var myConfigSection = app.Configuration.GetSection(MyRateLimitOptions.MyRateLimit);
|
199 | 201 | myConfigSection.Bind(myOptions);
|
200 | 202 |
|
201 |
| - |
202 | 203 | var options = new RateLimiterOptions()
|
203 | 204 | {
|
204 | 205 | OnRejected = (context, cancellationToken) =>
|
|
220 | 221 | .AddPolicy<string, SampleRateLimiterPolicy>(helloPolicy)
|
221 | 222 | .AddPolicy<string>(userPolicyName, context =>
|
222 | 223 | {
|
223 |
| - if (context.User?.Identity?.IsAuthenticated is not true) |
| 224 | + var username = "anonymous user"; |
| 225 | + if (context.User?.Identity?.IsAuthenticated is true) |
224 | 226 | {
|
225 |
| - var username = "anonymous user"; |
226 |
| - |
227 |
| - return RateLimitPartition.CreateSlidingWindowLimiter<string>(username, |
228 |
| - key => new SlidingWindowRateLimiterOptions( |
229 |
| - permitLimit: myOptions.permitLimit, |
230 |
| - queueProcessingOrder: QueueProcessingOrder.OldestFirst, |
231 |
| - queueLimit: myOptions.queueLimit, |
232 |
| - window: TimeSpan.FromSeconds(myOptions.window), |
233 |
| - segmentsPerWindow: myOptions.segmentsPerWindow |
234 |
| - )); |
235 |
| - } |
236 |
| - else |
237 |
| - { |
238 |
| - return RateLimitPartition.CreateNoLimiter<string>(string.Empty); |
| 227 | + username = context.User?.ToString()!; |
239 | 228 | }
|
| 229 | + |
| 230 | + return RateLimitPartition.CreateSlidingWindowLimiter<string>(username, |
| 231 | + key => new SlidingWindowRateLimiterOptions( |
| 232 | + permitLimit: myOptions.permitLimit, |
| 233 | + queueProcessingOrder: QueueProcessingOrder.OldestFirst, |
| 234 | + queueLimit: myOptions.queueLimit, |
| 235 | + window: TimeSpan.FromSeconds(myOptions.window), |
| 236 | + segmentsPerWindow: myOptions.segmentsPerWindow |
| 237 | + )); |
| 238 | + |
240 | 239 | });
|
241 | 240 |
|
242 | 241 | options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, IPAddress>(context =>
|
|
0 commit comments