using System; using System.Globalization; using System.Threading; using System.Threading.RateLimiting; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.RateLimiting; namespace API.Middleware.RateLimit; #nullable enable public class AuthenticationRateLimiterPolicy : IRateLimiterPolicy { public RateLimitPartition GetPartition(HttpContext httpContext) { return RateLimitPartition.GetFixedWindowLimiter(httpContext.Request.Headers.Host.ToString(), partition => new FixedWindowRateLimiterOptions { AutoReplenishment = true, PermitLimit = 1, Window = TimeSpan.FromMinutes(10), }); } public Func? OnRejected { get; } = (context, _) => { if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter)) { context.HttpContext.Response.Headers.RetryAfter = ((int) retryAfter.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo); } context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests; return new ValueTask(); }; }