Open Closed

AspNetCoreRateLimit not working #3064


0
JeffreyKhor created

Hi,

Below is my ABP details information:

ABP Framework version: v4.4.3

UI type: Angular

DB provider: EF Core

Tiered (MVC) or Identity Server Separated (Angular): yes

We would like to ask, we have been using the AspNetCoreRateLimit package from this link: https://www.nuget.org/packages/AspNetCoreRateLimit/

and we had been followed tutorial link as below: https://blog.elmah.io/rate-limiting-api-requests-with-asp-net-core-and-aspnetcoreratelimit/ https://www.c-sharpcorner.com/article/implement-rate-limiting-in-asp-net-core-web-api/ https://code-maze.com/aspnetcore-web-api-rate-limiting/

but still not working, its still allow concurrent sending request within milliseconds, anyone have been facing this issue before?

Regards, Jeffrey Khor


8 Answer(s)
  • 0
    maliming created
    Support Team

    hi

    Are there any error or warning logs?

  • 0
    JeffreyKhor created

    no error or any warning logs, below are files that i put in according to the tutorial:

    startup.cs:

    using AspNetCoreRateLimit;
    //using AspNetCoreRateLimit.Redis;
    using DevExpress.AspNetCore;
    using DevExpress.AspNetCore.Reporting;
    using DevExpress.AspNetCore.Reporting.QueryBuilder;
    using DevExpress.AspNetCore.Reporting.ReportDesigner;
    using DevExpress.AspNetCore.Reporting.WebDocumentViewer;
    using DevExpress.XtraReports.Security;
    using DevExpress.XtraReports.Services;
    //using DevExpress.XtraReports.Web.Extensions;
    //using DevExpress.XtraReports.Web.ReportDesigner.Services;
    //using DevExpress.XtraReports.Web.WebDocumentViewer;
    //using KMS.EntityFrameworkCore;
    using KMS.HubConfig;
    using KMS.Reports;
    //using KMS.Services;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    //using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    //using StackExchange.Redis;
    using System;
    //using System.Collections.Generic;
    //using Volo.Abp.Users;
    
    namespace KMS
    {
        public class Startup
        {
            private readonly IConfiguration appConfiguration;
    
            public Startup(IConfiguration _appConfiguration)
            {
                appConfiguration = _appConfiguration;
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddApplication<KMSHttpApiHostModule>();
    
                //added
                services.AddCors(options =>
                {
                    options.AddPolicy("AllowAllHeaders",
                        builder =>
                        {
                            builder.AllowAnyOrigin()
                                    .AllowAnyHeader()
                                    .AllowAnyMethod();
                        });
                });
    
                //added
                services.AddSignalR(options =>
                {
                    options.EnableDetailedErrors = true;
                });
    
                //added
                services.AddControllers();
    
                services.AddOptions();
                services.AddMemoryCache();
                services.Configure<IpRateLimitOptions>(appConfiguration.GetSection("IpRateLimit"));
                services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
                services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
                services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
                services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                services.AddInMemoryRateLimiting();
                //services.AddHttpContextAccessor();
    
                ScriptPermissionManager.GlobalInstance = new ScriptPermissionManager(ExecutionMode.Unrestricted);
            }
    
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
            {
                app.InitializeApplication();
    
                app.UseDevExpressControls();
                //added
                app.UseCors("AllowAllHeaders");
    
                app.UseIpRateLimiting();
    
                //added
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                    endpoints.MapHub<MyMessageHub>("/signalrkms");
                });
    
                DevExpress.XtraReports.Web.ClientControls.LoggerService.Initialize(ProcessException);
            }
    
            void ProcessException(Exception ex, string message)
            {
                // Log exceptions here. For instance:
                System.Diagnostics.Debug.WriteLine("[{0}]: Exception occured. Message: '{1}'. Exception Details:\r\n{2}",
                    DateTime.Now, message, ex);
            }
        }
    }
    
    appsettings.json:
    
    {
    ...
    "IpRateLimit": {
        "EnableEndpointRateLimiting": true,
        "StackBlockedRequests": false,
        "RealIPHeader": "X-Real-IP",
        "ClientIdHeader": "X-ClientId",
        "HttpStatusCode": 429,
        "GeneralRules": [
          {
            "Endpoint": "*:/api/app/kiosks",
            "Period": "30s",
            "Limit": 1
          }
        ]
      }
    }
    
  • 0
    maliming created
    Support Team

    There is no code in ABP that may affect this feature, you can try it without ABP framework.

  • 0
    JeffreyKhor created

    this is strange, i had tried on non ABP framework, no issue, and its working!!! only when with ABP framework causing issue

  • 0
    maliming created
    Support Team

    hi

    Can you share a project to reproduce the problem?

    You can use the free template project. https://abp.io/get-started

    [email protected]

  • 0
    JeffreyKhor created

    i had emailed you with the template project with the version 4.4.3, pls check ya, thanks in advance!!!

    Regards, Jeffrey Khor

  • 0
    maliming created
    Support Team

    ok, thanks I will check it.

  • 1
    maliming created
    Support Team

    hi

    Move the code from startup to module