stefanprodan / AspNetCoreRateLimit

ASP.NET Core rate limiting middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RateLimit is not working for each client_Id. Its always taking the "GeneralRules"

neotoni opened this issue · comments

Hello,

can you tell me what im doing wrong ?

    public void ConfigureServices(IServiceCollection services)
    {
        // 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<ClientRateLimitOptions>(Configuration.GetSection("ClientRateLimiting"));

        //load client rules from appsettings.json
        services.Configure<ClientRateLimitPolicies>(Configuration.GetSection("ClientRateLimitPolicies"));

        services.Configure<Clients>(Configuration.GetSection("Clients"));

        // configuration (resolvers, counter key builders)
        services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
        services.AddSingleton<IClientPolicyStore, MemoryCacheClientPolicyStore>();
        // inject counter and rules stores
        services.AddInMemoryRateLimiting();
        services.AddControllers();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        // To configure ( Parser 、 Counter key generator )
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseMiddleware<RequestHeaderMiddleware>();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseClientRateLimiting();
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "",
"Clients": {
"ClientsOK": [ "vdab-1", "vdab-2" ]
},
"ClientRateLimiting": {
"EnableEndpointRateLimiting": false,
"StackBlockedRequests": false,
"ClientIdHeader": "X-ClientId",
"RealIpHeader": "X-Real-Ip",
"HttpStatusCode": 429,
"EndpointWhitelist": [],
"ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
"GeneralRules": [
{
"Endpoint": "
",
"Period": "10s",
"Limit": 2
}

]

},

"ClientRateLimitPolicies": {
"ClientRules": [
{
"ClientId": "vdab-1",
"Rules": [
{
"Endpoint": "",
"Period": "10s",
"Limit": 7
},
{
"Endpoint": "
",
"Period": "15m",
"Limit": 200
}
]
},
{
"ClientId": "client-id-2",
"Rules": [
{
"Endpoint": "",
"Period": "1s",
"Limit": 5
},
{
"Endpoint": "
",
"Period": "15m",
"Limit": 150
},
{
"Endpoint": "*",
"Period": "12h",
"Limit": 500
}
]
}
]
}
}

ty

Hi @neotoni

Hope you doing good.

I am also facing same issues, Client policy not override general rules. Did you find any solutions on that?.

Hi Folks - Please help me

Hi there, are you seeding the ClientRateLimitPolicies in Program.cs with something similar to the snippet below?

public static async Task Main(string[] args)
{
    IWebHost webHost = CreateWebHostBuilder(args).Build();

    using (var scope = webHost.Services.CreateScope())
    {
         // get the ClientPolicyStore instance
         var clientPolicyStore = scope.ServiceProvider.GetRequiredService<IClientPolicyStore>();

         // seed client data from appsettings
         await clientPolicyStore.SeedAsync();
    }

    await webHost.RunAsync();
}