Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are using IIS 7 you could take a look at the <a href="http://learn.iis.net/page.aspx/548/using-dynamic-ip-restrictions/" rel="noreferrer">Dynamic IP Restrictions Extension</a>. Another possibility is to implement this as an action filter:</p> <pre><code>[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; } } } </code></pre> <p>And then decorate the action that needs to be limited:</p> <pre><code>[RateLimit(Seconds = 10)] public ActionResult Index() { return View(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload