Rate limit policy settings is not applied using AspNetCoreRateLimit in .net core 6
See original GitHub issueHi, I am using AspNetCoreRateLimit version 4.0.1 and I have done all the setup in .net core 6 web api. I can see rate limit is working when I send a call via postman.
However, when I add IpRateLimitPolicies with specific IP address, the settings won’t be applied. I use postman and this time in the proxy I added the ip address to 127.0.0.1. I can see the ip hitting the api is set correctly when I use Request.HttpContext.Connection.RemoteIpAddress; I also deployed to our dev environment and called from a different client and got the same result.
I registered them as follow in program.cs:
_serviceCollection.AddOptions(); _serviceCollection.AddMemoryCache(); _serviceCollection.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting")); _serviceCollection.Configure<IpRateLimitPolicies>(builder.Configuration.GetSection("IpRateLimitPolicies")); _serviceCollection.AddInMemoryRateLimiting(); _serviceCollection.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>(); _serviceCollection.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>(); _serviceCollection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); _serviceCollection.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
Also added: app.UseIpRateLimiting();
My appsettings also looks like:
{ "IpRateLimiting": { "EnableEndpointRateLimiting": false, "StackBlockedRequests": false, "RealIPHeader": "X-Real-IP", "ClientIdHeader": "X-ClientId", "IpWhitelist": [ ], "EndpointWhitelist": [], "ClientWhitelist": [], "HttpStatusCode": 429, "GeneralRules": [ { "Endpoint": "*", "Period": "10s", "Limit": 1 } ] }, "IpRateLimitPolicies": { "IpRules": [ { "Ip": "127.0.0.1", "Rules": [ { "Endpoint": "*", "Period": "20s", "Limit": 2 } ] } ] }
But apparently the settings under IpRateLimitPolicies won’t be applied.
I wonder if I have missed anything here?
Thank you
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:13
Top GitHub Comments
Same issue here, my ratelimit rules are not applying either
@arash3003 yes,you need add those code at Program.cs
var ipPolicyStore = app.Services.GetRequiredService<IIpPolicyStore>(); ipPolicyStore.SeedAsync().GetAwaiter().GetResult(); var clientPolicyStore = app.Services.GetRequiredService<IClientPolicyStore>(); clientPolicyStore.SeedAsync().GetAwaiter().GetResult();
and if you test at localhost,try change “Ip”: “127.0.0.1” to “Ip”: “::1/10” ,it may help you.