如何在 ASP.NET MVC 站点中实施速率限制?
我正在构建一个 ASP.NET MVC 站点,我想在其中限制经过身份验证的用户使用该站点某些功能的频率。
尽管我了解速率限制从根本上是如何工作的,但我无法想象如何在不产生主要代码味道的情况下以编程方式实现它。
您能否使用 C# 示例代码为我指出一个简单但功能强大的解决方案来解决此类问题?
如果重要的话,所有这些功能目前都表示为仅接受 HTTP POST
的操作。我最终可能还想对 HTTP GET
函数实现速率限制,因此我正在寻找一种适用于所有此类情况的解决方案。
I'm building an ASP.NET MVC site where I want to limit how often authenticated users can use some functions of the site.
Although I understand how rate-limiting works fundamentally, I can't visualize how to implement it programatically without creating a major code smell.
Can you point me towards a simple yet powerful solution for approaching such a problem, with C# sample code?
If it matters, all of these functions are currently expressed as Actions that only accept HTTP POST
. I may eventually want to implement rate-limiting for HTTP GET
functions as well, so I'm looking for a solution that works for all such circumstances.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 IIS 7,您可以查看动态IP 限制扩展。另一种可能性是将其实现为动作过滤器:
然后装饰需要限制的动作:
If you are using IIS 7 you could take a look at the Dynamic IP Restrictions Extension. Another possibility is to implement this as an action filter:
And then decorate the action that needs to be limited:
看看 Jarrod 的回答,了解他们如何在 SO 上做到这一点。
StackOverflow MVC 限制
一些示例代码以及其工作原理的解释。
Have a look at Jarrod's answer on how they do this on SO.
StackOverflow MVC Throttling
Some example code as well as explanation on how it works.
.Net 6
看看:
Nuget:
https://www.nuget.org/packages/DotNetRateLimiter/
这很简单
:甚至您可以通过路由或查询参数控制请求:
.Net 6
Check it out:
Nuget:
https://www.nuget.org/packages/DotNetRateLimiter/
It is simple:
And even you can control request by route or query parameters: