Asp 网络核心速率限制端点模式不限制特定控制器

问题描述 投票:0回答:1

除了 * 之外,似乎无法获得任何端点模式来进行任何速率限制,我做错了什么?我正在使用 .NET Core 6,目前无法迁移到 7。

new RateLimitRule { Endpoint = "*", Limit = 10, Period = "10s"}, //works!!
new RateLimitRule { Endpoint = "get:/api/waternet/*", Limit = 3, Period = "10s"}, // doesnt work
new RateLimitRule { Endpoint = "*:/api/waternet/*", Limit = 3, Period = "10s"}, // doesn't work

以下代码

我的控制器

[Route("api/[controller]")]
[ApiController]
public class WaternetController : ControllerBase
{

    [HttpGet]
    public async Task<ActionResult<List<APIGetWaternetModel>>> GetFlowData(string companyGuid, string serial = null, string startDate = null, string endDate = null, string sitename = null)
    {
        try
        {
            Guid.Parse(companyGuid);
        }
        return ... etc
     }
}

服务扩展

public static class ServiceExtensions
{
    public static void ConfigureRateLimitingOptions(this IServiceCollection services)
    {
        //override these with specific rules set up in appsettings (see doc WaternetAPI Rate Throttling Notes)
        var rateLimitRules = new List<RateLimitRule>
          {
              //new RateLimitRule { Endpoint = "*", Limit = 10, Period = "10s"}, //works!!
              //new RateLimitRule { Endpoint = "get:/api/waternet/*", Limit = 3, Period = "10s"}, // doesnt work
              new RateLimitRule { Endpoint = "*:/api/waternet/*", Limit = 3, Period = "10s"}, // doesnt work

          };

        services.Configure<IpRateLimitOptions>(opt => { opt.GeneralRules = rateLimitRules; });
        services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
        services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
        services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
        services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
    }
}

我的创业

public void ConfigureServices(IServiceCollection services)
{

    //needed for API rate throttling
    services.AddMemoryCache();
    services.ConfigureRateLimitingOptions(); //from ServiceExtensions.cs
    services.AddHttpContextAccessor();

    services.AddControllers(options =>
    {
        options.Conventions.Add(new GroupingByNamespaceConvention());
    });


}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

    // Needs to be before any middleware is loaded
    app.UseRouting();

    //API rate throttling
    app.UseIpRateLimiting();

}
.net-core rate-limiting
1个回答
0
投票

services.Configure(opt => { opt.GeneralRules =rateLimitRules; opt.EnableEndpointRateLimiting = true; });

© www.soinside.com 2019 - 2024. All rights reserved.