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("<table width='778' border='0' align='center' cellpadding='3' cellspacing='2' bgcolor='#009900' style='font-size: 14px; '>");
    Response.Write(" <tr bgcolor='#FFFFFF'>");
    Response.Write("  <td><img src='/newimages/logos.gif'></td>");
    Response.Write("  <td bgcolor='#EEFFEE'为了保证您的访问安全,请您 " + freetime + " 秒后<a href='" + Request.RawUrl + "' target='_self' style='color:#FF0000;'>点击这里刷新</a>此页面</td>");
    Response.Write(" </tr>");
    Response.Write("</table>");
    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("<table width='778' border='0' align='center' cellpadding='3' cellspacing='2' bgcolor='#009900' style='font-size: 14px; '>");
    Response.Write(" <tr bgcolor='#FFFFFF'>");
    Response.Write("  <td><img src='/newimages/logos.gif'></td>");
    Response.Write("  <td bgcolor='#EEFFEE'为了保证您的访问安全,页面将在2秒后将自动跳转到您要访问的内容!</td>");
    Response.Write(" </tr>");
    Response.Write("</table>");
    Response.Write("<meta http-equiv=\"refresh\" content=\"2\";URL=" + HttpContext.Current.Request.Cookies["page"].Value.ToString() + ">");
    Response.End();
}

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

分享名称:asp.net网站防恶意刷新的Cookies与Session如何解决-创新互联
网页地址:https://www.cdcxhl.com/article46/pjeeg.html

成都网站建设公司_创新互联,为您提供网站改版品牌网站建设用户体验App设计外贸网站建设品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化