gpt4 book ai didi

asp.net-core - 为什么在将这个 AspNetCoreRateLimit 中间件用于 Asp.Net Core 时无法获得启动的速率限制

转载 作者:行者123 更新时间:2023-12-03 23:09:21 24 4
gpt4 key购买 nike

有人可以告诉我我做错了什么......我正在使用这个中间件来限制客户端可以对我的端点进行的 API 调用的数量:
https://github.com/stefanprodan/AspNetCoreRateLimit/wiki/IpRateLimitMiddleware

我无法启动此速率限制。这是我的 Startup.cs(相关部分):

public void ConfigureServices(IServiceCollection services)
{
// 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>();

// 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>();

// Configure strongly-typed configuration settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();

services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
corsBuilder => corsBuilder.WithOrigins("*")
.AllowAnyMethod()
.AllowAnyHeader());
});

services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
// Ensure that the CORS call is before UseMvc
app.UseCors("AllowSpecificOrigin");

loggerFactory.AddLog4Net();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseIpRateLimiting();

app.UseHttpsRedirection();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

}

还有我的 appsettings.json 文件:
"IpRateLimiting": {
"EnableEndpointRateLimiting": true,
"StackBlockedRequests": false,
"RealIPHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"GeneralRules": [
{
"Endpoint": "*",
"Period": "10s",
"Limit": 1
}
]
}

仅供引用,我已经尝试将 EnableEndpointRateLimiting 设置为“true”和“false”,但都没有效果。

我的测试 API 调用都在进行中……当我预计会受到限制时!

最佳答案

事实证明,这对我来说是一个非常愚蠢的错误。我有 IpRateLimiting session 嵌套在我的 appsettings.json 文件的“AppSettings”部分中,而它应该在根级别指定。

关于asp.net-core - 为什么在将这个 AspNetCoreRateLimit 中间件用于 Asp.Net Core 时无法获得启动的速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59684967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com