asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法
本文實(shí)例講述了asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法,是WEB程序設(shè)計(jì)中非常實(shí)用的技巧。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
Session版實(shí)現(xiàn)方法:
public double time; public const int freetime = 1;//防刷冰凍時(shí)間間隔,當(dāng)前為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'為了保證您的訪問(wèn)安全,請(qǐng)您 " + freetime + " 秒后<a href='" + Request.RawUrl + "' target='_self' style='color:#FF0000;'>點(diǎn)擊這里刷新</a>此頁(yè)面</td>"); Response.Write(" </tr>"); Response.Write("</table>"); Response.End(); }
Cookies版實(shí)現(xiàn)方法:
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中存儲(chǔ)的url值 } string strThisPage = HttpContext.Current.Request.Url.PathAndQuery.ToString();//獲取當(dāng)前頁(yè)地址 DateTime LastTime = DateTime.Now; if (page.Equals(strThisPage))//如果cookie中的值和當(dāng)前頁(yè)相等,那么表示是刷新操作 { TimeSpan ts = LastTime - DateTime.Parse(HttpContext.Current.Request.Cookies["time"].Value.ToString()); time = ts.Seconds; if (time < freetime) { warm_prompt(); } } else { //執(zhí)行操作 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'為了保證您的訪問(wèn)安全,頁(yè)面將在2秒后將自動(dòng)跳轉(zhuǎn)到您要訪問(wèn)的內(nèi)容!</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(); }
相信本文所述對(duì)大家的asp.net程序設(shè)計(jì)有一定的借鑒價(jià)值。
相關(guān)文章
Visual Studio(VS2017)配置C/C++ PostgreSQL9.6.3開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Visual Studio(VS2017)配置C/C++,PostgreSQL9.6.3開發(fā)環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼...2007-04-04在IIS上重新注冊(cè).NET Framework 2.0的命令和參數(shù)詳解
這篇文章主要介紹了在IIS上重新注冊(cè).NET Framework 2.0的命令和參數(shù)詳解,但其它.NET Framework 版本沒(méi)有測(cè)試,需要的朋友可以參考下2014-07-07ADO.Net對(duì)oracle數(shù)據(jù)庫(kù)操作的實(shí)例代碼
ADO.Net對(duì)oracle數(shù)據(jù)庫(kù)操作的實(shí)例代碼,需要的朋友可以參考一下2013-06-06javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼
javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05asp.net FileUpload控件實(shí)現(xiàn)文件格式判斷與文件大小限制
這篇文章主要介紹了有關(guān)asp.net fileupload控件判斷文件格式,以及進(jìn)行文件大小限制的方法,可以在web.config中配置,也可以在.cs文件中實(shí)現(xiàn),需要的朋友參考下2014-11-11ASP.NET生成兩個(gè)日期范圍內(nèi)隨機(jī)時(shí)間的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET生成兩個(gè)日期范圍內(nèi)隨機(jī)時(shí)間的實(shí)現(xiàn)方法,通過(guò)自定義函數(shù)記錄開始時(shí)間與結(jié)束時(shí)間確定時(shí)間范圍進(jìn)而生成該時(shí)間段的隨機(jī)時(shí)間,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12