From 253f981012e93e8a64cfaa39a1ba81f8f7f16176 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 15 Aug 2026 23:02:03 +0200 Subject: [PATCH] RateLimitingDotNET8: target net10.0, fix rejection status and auth partitioner - Retarget app and test projects to net10.0; refresh NuGet package versions. - Set RejectionStatusCode to 429 on the fixed window registration so the documented limiters return 429 on their own instead of the framework default 503. - Replace the blocking GetTokenAsync().Result call in the authorization partitioner with a synchronous read of the authenticated user identity. --- .../RateLimitingDotNET8.Tests.csproj | 12 ++++++------ .../RateLimitingDotNET8/RateLimiters.cs | 18 ++++++++++-------- .../RateLimitingDotNET8.csproj | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) 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 - +