【问题标题】:Not getting status code 429 and customized error message while using AspNetCoreRateLimit使用 AspNetCoreRateLimit 时未获得状态代码 429 和自定义错误消息
【发布时间】:2020-05-13 22:07:02
【问题描述】:

我使用 AspNetCoreRateLimit nuget 包在我的 .net 核心 Api 应用程序中实现了限速中间件。我在 appsettings.json 文件中设置的规则正在运行。但是我没有收到响应代码 429 和自定义消息。而是在状态列中显示 (failed)net::ERR_FAILED。 这是我的启动文件中的代码。

services.AddOptions();
services.AddMemoryCache();
services.Configure<IpRateLimitOptions>(config.GetSection("IpRateLimiting"));
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
services.AddHttpContextAccessor();

这是来自 appsetting.json 文件的配置数据

 "IpRateLimiting": {
    "EnableEndpointRateLimiting": true,
    "StackBlockedRequests": false,
    "RealIpHeader": "X-Real-IP",
    "ClientIdHeader": "X-ClientId",
    "HttpStatusCode": 429,
    "QuotaExceededMessage": "API calls quota exceeded",
    "GeneralRules": [
      {
        "Endpoint": "*:/api/*",
        "Period": "1m",
        "Limit": 20
      }
    ]
  }

在这里,我的请求在第 20 次尝试后被阻止,但是我既没有收到 429 的响应代码,也没有收到“API 调用配额超出”消息集在 QuotaExceededMessage 属性中。 我错过了什么吗?

这是我的 Startup.cs 文件中的代码

public void ConfigureServices(IServiceCollection services)
        {

            services.AddAuthentication(IISDefaults.AuthenticationScheme);
        //This is where i have configured the Rate limit settings using extension method
           services.ConfigureRateLimitSettings(Configuration);  

            services.AddMvc(options =
            {
        options.RespectBrowserAcceptHeader = true;
                options.ReturnHttpNotAcceptable = true;
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddHttpContextAccessor();
            services.ConfigureCORS(Configuration);
            services.ConfigureSwagger();
            services.ConfigureDBContext(Configuration);
            services.AddAutoMapper(typeof(Startup));
        }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseIpRateLimiting();
            app.UseHttpsRedirection();
            app.UseCORSExtension();
            app.UseAuthentication();
            app.UseStaticFilesExtension(Configuration);
            app.UseMvc();
            app.UseSwaggerExtension();
        }

这里是 ConfigureRateLimitSettings 的扩展方法

public static void ConfigureRateLimitSettings(this IServiceCollection services, IConfiguration config)
   {
                services.AddOptions();
                services.AddMemoryCache();
                services.Configure<IpRateLimitOptions>(config.GetSection("IpRateLimiting"));
                services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
                services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
                services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
                //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }

【问题讨论】:

  • 你能把Startup.cs中的所有代码都加进去吗?
  • @dustyhoppe : 我已经从我的启动文件中添加了代码。

标签: .net-core


【解决方案1】:

我能够解决这个问题。我刚刚改变了我的 app.UseIpRateLimiting(); 的位置我把它放在 app.UseCORSExtension() 和 app.UseAuthentication() 之后;

所以现在我的代码如下

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCORSExtension();
        app.UseAuthentication();
        app.UseIpRateLimiting(); -- Placed it here
        app.UseStaticFilesExtension(Configuration);
        app.UseMvc();
        app.UseSwaggerExtension();
    }

现在我可以得到响应代码 429 Too Many Requests。 感谢大家的宝贵时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多