asp.net网站防恶意刷新的Cookies与Session解决方法
程序员文章站
2024-03-31 12:12:52
本文实例讲述了asp.net网站防恶意刷新的cookies与session解决方法,是web程序设计中非常实用的技巧。分享给大家供大家参考。具体实现方法如下:
sessi...
本文实例讲述了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("<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程序设计有一定的借鉴价值。