当前位置:首页 > 利用Cache限制页面频繁请求(最少要等N秒)

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

点击次数:1299  更新日期:2010-12-27
\n

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)) // 赞助用户不作限制
{


\n

nextTitle = “你要查看的页面”;
nextUrl = Request.RawUrl;
message = “对不起,你的点击或刷新页面太快,服务器无法完成请求。请点击或刷新页面之间的时间不要小于10秒。赞助用户无此限制。谢谢!”;
Util.RedirectToInfoPage(InfoIcon.Stop, message, nextTitle, nextUrl);
return;

}
}
}


\n

来源:阿良.NET

\n