如何在ASP.NET MVC站点中实现速率限制?

我正在构建一个ASP.NET MVC站点,我想限制经过身份validation的用户使用该站点的某些function的频率。

虽然我理解速率限制如何从根本上解决,但我无法想象如何以编程方式实现它而不会产生主要的代码气味。

能用C#示例代码指出一个简单而强大的解决方案来解决这个问题吗?

如果重要,所有这些函数当前都表示为仅接受HTTP POST Actions。 我最终可能希望为HTTP GET函数实现速率限制,所以我正在寻找适用于所有这些情况的解决方案。

如果您使用的是IIS 7,则可以查看动态IP限制扩展 。 另一种可能性是将其实现为动作filter:

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class RateLimitAttribute : ActionFilterAttribute { public int Seconds { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { // Using the IP Address here as part of the key but you could modify // and use the username if you are going to limit only authenticated users // filterContext.HttpContext.User.Identity.Name var key = string.Format("{0}-{1}-{2}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName, filterContext.HttpContext.Request.UserHostAddress ); var allowExecute = false; if (HttpRuntime.Cache[key] == null) { HttpRuntime.Cache.Add(key, true, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration, CacheItemPriority.Low, null); allowExecute = true; } if (!allowExecute) { filterContext.Result = new ContentResult { Content = string.Format("You can call this every {0} seconds", Seconds) }; filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict; } } } 

然后装饰需要限制的动作:

 [RateLimit(Seconds = 10)] public ActionResult Index() { return View(); } 

看看Jarrod关于他们如何在SO上做到这一点的答案。

StackOverflow MVC Throttling

一些示例代码以及它如何工作的解释。