成都网站建设设计

将想法与焦点和您一起共享

asp.net网站防恶意刷新的Cookies与Session如何解决-创新互联

这篇文章主要讲解了“asp.net网站防恶意刷新的Cookies与Session如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“asp.net网站防恶意刷新的Cookies与Session如何解决”吧!

为孙吴等地区用户提供了全套网页设计制作服务,及孙吴网站建设行业解决方案。主营业务为做网站、成都网站设计、孙吴网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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.Write(" ");
    Response.Write("  ");
    Response.Write("  点击这里刷新此页面");
    Response.Write(" ");
    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.Write("  ");
    Response.Write(" ");
    Response.Write("");
    Response.Write("");
    Response.End();
}

感谢各位的阅读,以上就是“asp.net网站防恶意刷新的Cookies与Session如何解决”的内容了,经过本文的学习后,相信大家对asp.net网站防恶意刷新的Cookies与Session如何解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前标题:asp.net网站防恶意刷新的Cookies与Session如何解决-创新互联
文章地址:http://chengdu.cdxwcx.cn/article/pjeeg.html