1

I installed AspNetCoreRateLimit and am trying to get it configured properly, but It's not having any effect on the API.

In Startup.cs, in ConfigureServices() I added this

#region AspNetCoreRateLimit
            // needed to load configuration from appsettings.json
            services.AddOptions();

            // needed to store rate limit counters and ip rules
            services.AddMemoryCache();

            //load general configuration from appsettings.json
            services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));

            // inject counter and rules stores
            services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
            services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();

            // Add framework services.
            services.AddMvc();

            // https://github.com/aspnet/Hosting/issues/793
            // the IHttpContextAccessor service is not registered by default.
            // the clientId/clientIp resolvers use it.
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            // configuration (resolvers, counter key builders)
            services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
#endregion AspNetCoreRateLimit

and in Configure() of the same file I put

app.UseIpRateLimiting();

In appsettings.json I added this section, making sure it's in the object root

  "IpRateLimiting": {
    "EnableEndpointRateLimiting": true,
    "StackBlockedRequests": false,
    "RealIpHeader": "X-Real-IP",
    "ClientIdHeader": "X-ClientId",
    "HttpStatusCode": 429,
    //"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ],
    //"EndpointWhitelist": [ "get:/api/license", "*:/api/status" ],
    //"ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
    "GeneralRules": [
      {
        "Endpoint": "*",
        "Period": "5m",
        "Limit": 1
      }
    ]
  }

For testing purposes I have it set to 1 call every 5 minutes for the entire API, but I can easily make the same call multiple times in a row with no issues.

Can anyone tell me what I'm doing wrong here?

2 Answers 2

2

I just had a similar issue but not using IP limiting. It was working before then stopped, but it would've been because I've added services since then.

In your ConfigureServices make sure all the registrations are above any other services with the IHttpContextAccessor and IRateLimitConfiguration are last.

In the Configure method try app.UseIpRateLimiting(); before everything else.

You can see a startup class here.

https://github.com/stefanprodan/AspNetCoreRateLimit/wiki/ClientRateLimitMiddleware

1
  • You are genius! it worked for me! moving this services before everyother service init. :D why this is not marked as accepted answer?
    – Parth
    Nov 29 at 13:15
0

Try Replace the following lines

  // inject counter and rules stores
  services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
  services.AddSingleton<IRateLimitCounterStore, 
  MemoryCacheRateLimitCounterStore>();

with

services.AddInMemoryRateLimiting(); 

this worked for me in a sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.