Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能';无法获取AspNetCoreRateLimit以限制API调用_C#_.net Core_Api Design - Fatal编程技术网

C# 我能';无法获取AspNetCoreRateLimit以限制API调用

C# 我能';无法获取AspNetCoreRateLimit以限制API调用,c#,.net-core,api-design,C#,.net Core,Api Design,我安装了AspNetCoreRateLimit,并试图将其正确配置,但它对API没有任何影响 在Startup.cs中,我在ConfigureServices()中添加了以下内容 #region AspNetCoreRateLimit // needed to load configuration from appsettings.json services.AddOptions(); // needed to store

我安装了AspNetCoreRateLimit,并试图将其正确配置,但它对API没有任何影响

在Startup.cs中,我在ConfigureServices()中添加了以下内容

#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
在appsettings.json中,我添加了这个部分,确保它位于对象根目录中

  "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
      }
    ]
  }
出于测试目的,我将整个API设置为每5分钟调用一次,但我可以轻松地连续多次调用相同的API,而不会出现任何问题


谁能告诉我我做错了什么吗?

我刚刚遇到了一个类似的问题,但没有使用IP限制。它在停止之前是工作的,但那是因为我从那时起就添加了服务

ConfigureServices
中,确保使用
IHttpContextAccessor
IRateLimitConfiguration
进行的所有注册高于任何其他服务

在Configure方法中,尝试
app.UseIpRateLimiting()优先于其他一切

您可以在这里看到一个startup类

  "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
      }
    ]
  }