AspNetCoreRateLimit有IP限流和客户端ID限制,这里只分享IP方式。
1.先安装AspNetCoreRateLimit

2.注入服务
#region Ip限流
//需要从加载配置文件appsettings.JSON
services.AddOptions();
//需要存储速率限制计算器和ip规则
services.AddMemoryCache();
// IpRateLimiting 配置
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
// 注册默认的 ProcessingStrategy
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
//从appsettings.json中加载Ip规则
//services.Configure<IpRateLimitPolicies>(Configuration.Ge
在.NET中使用AspCoreRateLimit进行IP和客户端ID限流配置

最低0.47元/天 解锁文章
405

被折叠的 条评论
为什么被折叠?



