本文实例讲述了asp.net网站防恶意刷新的Cookies与Session解决方法,是WEB程序设计中非常实用的技巧。分享给大家供大家参考。具体实现方法如下:
Session版实现方法:
public double time;
public const int freetime = 1;//防刷冰冻时间间隔,当前为1秒
#region 防恶意刷新
if (Session.SessionID == null)
{
Response.End();
}
else if (Session["sionid"] == null)
{
Session["sionid"] = Session.SessionID;
}
if (Session["last"] == null)
{
Session["last"] = DateTime.Now;
}
else
{
DateTime thisTime = DateTime.Now;
DateTime lastTime = DateTime.Parse(Session["last"].ToString());
if (Session.SessionID == Session["sionid"].ToString())
Session["last"] = thisTime;
TimeSpan ts = thisTime - lastTime;
time = ts.TotalMilliseconds;
if (time < freetime * 500)
{
warm_prompt();
}
}
#endregion
public void warm_prompt()
{
Response.Write("
"); | 点击这里刷新此页面 | ");
Response.End();
}
Cookies版实现方法:
public double time;
public const int freetime = 2;
#region 防恶意刷新
string page;
if (Request.Cookies["page"] == null)
{
page = "";
}
else
{
page = HttpContext.Current.Request.Cookies["page"].Value.ToString(); //获取cookie中存储的url值
}
string strThisPage = HttpContext.Current.Request.Url.PathAndQuery.ToString();//获取当前页地址
DateTime LastTime = DateTime.Now;
if (page.Equals(strThisPage))//如果cookie中的值和当前页相等,那么表示是刷新操作
{
TimeSpan ts = LastTime - DateTime.Parse(HttpContext.Current.Request.Cookies["time"].Value.ToString());
time = ts.Seconds;
if (time < freetime)
{
warm_prompt();
}
}
else
{
//执行操作
Response.Cookies["page"].Value = strThisPage;
Response.Cookies["time"].Value = LastTime.ToString();
}
#endregion
public void warm_prompt()
{
Response.Write("
"); | ");
Response.Write(" |
Response.Write("");
Response.End();
}
更多信息请查看IT技术专栏