利用Cache限制页面频繁请求(最少要等N秒)

来源:互联网 发布:口红 房地产 知乎 编辑:程序博客网 时间:2024/05/17 08:28

string ip = WebUtil.GetIPString();if (path.StartsWith("/article/download.aspx")) //在Application_BeginRequest 判断是否需要限制的页,如果是特定页面在Page_Load中不用判断了。{    string key = "请求时间限制_" + ip;    if (Context.Cache[key] == null)    {        Context.Cache.Insert(key, 1, null,            Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 10));//10秒限制    }    else    {        Visitor visitor = Visitor.Current;        if (!visitor.HasPermission(Permissions.Sponser)) // 赞助用户不作限制        {                nextTitle = "你要查看的页面";                nextUrl = Request.RawUrl;                message = "对不起,你的点击或刷新页面太快,服务器无法完成请求。请点击或刷新页面之间的时间不要小于10秒。赞助用户无此限制。谢谢!";                Util.RedirectToInfoPage(InfoIcon.Stop, message, nextTitle, nextUrl);                return;                     }    }}