我正在使用带有Asp.Net Core 2.2 Web API的AspNetCoreRateLimit库。我已经将IpRateLimiting与Startup.cs
中的默认设置一起使用,如AspNetCoreRateLimit wiki所示。
我具有带有查询参数的API端点,并且它与http GET查询一起使用,如下所示(请参阅参数startDate
和stopDate
):
GET "https://host/api/endpoint/path?startDate=2020-04-04&stopDate=2020-04-04"
我想仅将unique个请求(具有唯一的参数组合)限制为每小时5个请求。因此,例如,以下情况应在1小时内出现:
5 times: GET "https://host/api/endpoint/path?startDate=2020-04-04&stopDate=2020-04-04"
5 times: GET "https://host/api/endpoint/path?startDate=2020-04-05&stopDate=2020-04-05"
问题在于,无论参数如何,我每小时只能发送total 5个请求。
以下是我来自appsettings.json的IpRateLimiting设置。
"IpRateLimiting": {
"EnableEndpointRateLimiting": true,
"StackBlockedRequests": false,
"RealIPHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"GeneralRules": [
{
"Endpoint": "*:/api/endpoint/path",
"Period": "1h",
"Limit": 5
}
]
}
[请注意,我不想更改[[@Yongqing Yu]]的this好的答案中所提议的端点路由,因为那里有很多使用我的API的API客户端,想要引入任何重大更改。
我正在将AspNetCoreRateLimit库与Asp.Net Core 2.2 Web API结合使用。我已经将IpRateLimiting与Startup.cs中的默认设置一起使用,如AspNetCoreRateLimit Wiki所示。我有API端点...change the route
,并将参数直接转换为路径的一部分,例如'https://host/api/endpoint/path/2020-04-04/2020-04-04'
,以便Endpoint
中的GeneralRules
可以通过*
满足您的条件。如上所述here,可以实现自己的路径提取逻辑。我编写了自定义IpRateLimitMiddleware并重写了ResolveIdentity方法,如下所示: