diff --git a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8.Tests/RateLimitingDotNET8.Tests.csproj b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8.Tests/RateLimitingDotNET8.Tests.csproj index 29769604fb..b359aba1e0 100644 --- a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8.Tests/RateLimitingDotNET8.Tests.csproj +++ b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8.Tests/RateLimitingDotNET8.Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable @@ -10,14 +10,14 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimiters.cs b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimiters.cs index 34c6530a33..37761de981 100644 --- a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimiters.cs +++ b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimiters.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.RateLimiting; +using Microsoft.AspNetCore.RateLimiting; using Microsoft.Extensions.Options; using System.Threading.RateLimiting; @@ -11,14 +10,17 @@ public static void FixedRateLimiter(WebApplicationBuilder builder) { var fixedOptions = GetOptionValues(builder); - builder.Services.AddRateLimiter(options => options - .AddFixedWindowLimiter(policyName: Policies.Fixed, limiterOptions => + builder.Services.AddRateLimiter(options => + { + options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; + options.AddFixedWindowLimiter(policyName: Policies.Fixed, limiterOptions => { limiterOptions.PermitLimit = fixedOptions!.PermitLimit; limiterOptions.Window = TimeSpan.FromMinutes(fixedOptions.Window); limiterOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; limiterOptions.QueueLimit = fixedOptions.QueueLimit; - })); + }); + }); } public static void SlidingRateLimiter(WebApplicationBuilder builder) @@ -79,10 +81,10 @@ public static void AuthorizationRateLimiter(WebApplicationBuilder builder) limiterOptions.RejectionStatusCode = StatusCodes.Status429TooManyRequests; limiterOptions.AddPolicy(policyName: Policies.Authorization, partitioner: httpContext => { - var accessToken = httpContext.GetTokenAsync("access_token").Result; + var username = httpContext.User.Identity?.Name; - return !string.IsNullOrEmpty(accessToken) - ? RateLimitPartition.GetFixedWindowLimiter(accessToken, options => + return !string.IsNullOrEmpty(username) + ? RateLimitPartition.GetFixedWindowLimiter(username, options => new FixedWindowRateLimiterOptions { QueueLimit = authorizedLimiterOptions!.QueueLimit, diff --git a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimitingDotNET8.csproj b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimitingDotNET8.csproj index 099e4bd480..98a9ed22f4 100644 --- a/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimitingDotNET8.csproj +++ b/aspnetcore-webapi/RateLimitingDotNET8/RateLimitingDotNET8/RateLimitingDotNET8.csproj @@ -1,13 +1,13 @@  - net8.0 + net10.0 enable enable - +