/// <summary>
/// Decorates any MVC route that needs to have client requests limited by time.
/// </summary>
/// <remarks>
/// Uses the current System.Web.Caching.Cache to store each client request to the decorated route.
/// </remarks>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ThrottleAttribute : ActionFilterAttribute
{
/// <summary>
/// A unique name for this Throttle.
/// </summary>
/// <remarks>
/// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1"
/// </remarks>
public string Name { get; set; } /// <summary>
/// The number of seconds clients must wait before executing this decorated route again.
/// </summary>
public int Seconds { get; set; } /// <summary>
/// A text message that will be sent to the client upon throttling. You can include the token {n} to
/// show this.Seconds in the message, e.g. "Wait {n} seconds before trying again".
/// </summary>
public string Message { get; set; } public override void OnActionExecuting(ActionExecutingContext c)
{
var key = string.Concat(Name, "-", c.HttpContext.Request.UserHostAddress);
var allowExecute = false; if (HttpRuntime.Cache[key] == null)
{
HttpRuntime.Cache.Add(key,
true, // is this the smallest data we can have?
null, // no dependencies
DateTime.Now.AddSeconds(Seconds), // absolute expiration
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null); // no callback allowExecute = true;
} if (!allowExecute)
{
if (String.IsNullOrEmpty(Message))
Message = "You may only perform this action every {n} seconds."; c.Result = new ContentResult { Content = Message.Replace("{n}", Seconds.ToString()) };
// see 409 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
c.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict;
}
}
}
[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
return Content("TestThrottle executed");
}

ThrottleAttribute的更多相关文章

随机推荐

  1. python正则表达式

    group 组是通过 "(" 和 ")" 元字符来标识的. "(" 和 ")" 有很多在数学表达式中相同的意思:它们一起 ...

  2. libevent之丢失header问题

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ 背景 分享一个hhvm使用http server方式来处理请求的问题及对应的patch.hhvm3+版本支持fas ...

  3. PL-SQL(免安装版本)报错ORA-12154

      今天在帮同事安装PL/SQL时,在登陆的时候出现上述错误,从网上找了好多解决方法,但都没有解决问题.对于免安装版本的PL/SQL在登陆是应该先配置好路径:bin\instantclient_11_ ...

  4. windows 7 右下角登陆信息去除

    ---恢复内容开始--- 开始--运行中输入regedit 找到注册表键值HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NlaSvc\Par ...

  5. Python 定位字符串

    一位朋友在玩闯关游戏时遇到如下问题: 感觉考查的就是字符串操作,用string模块就可完成:代码如下: # -*- coding: utf-8 -*- __author__ = 'Evilxr' im ...

  6. Ret2Libc 练习(1) -- ZwSetInformationProcess

    花了两个小半晚上的时间将0day安全这本书的绕过DEP的第一个实验做了,这里做些笔记. Ret2libc 我现在自己的理解就是在开启DEP保护的情况下,在程序的其他的可执行位置找到可以满足我利用要求的 ...

  7. VIM常用快捷键

    光标前插入i,行首插入 拷贝当前行 yy或者Y 删除一行dd,删除后进入插入模式cc或者S 粘贴p 撤销u,重做ctrl + r 删除一行dd,删除后进入插入模式cc或者S

  8. vm centos 网络配置

    安装Centos系统,查看网络配置. 输入命令:ifconfig 127.0.0.1 要开启网络 进入ifcfg-eth0文件. 输入命令:vi /etc/sysconfig/network-scri ...

  9. SQL Server Profiler

    一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...

  10. mysql操作记录

    use mysql;select host,user,password from user; grant all privileges on *.* to root@'%' identified by ...