温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-02 15:26:40 来源:亿速云 阅读:150 作者:chen 栏目:开发技术

这篇文章主要讲解了“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如何解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI