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'為了保證您的訪問安全,請(qǐng)您 " + freetime + " 秒后<a href='" + Request.RawUrl + "' target='_self' style='color:#FF0000;'>點(diǎn)擊這里刷新</a>此頁面</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)前頁地址
DateTime LastTime = DateTime.Now;
if (page.Equals(strThisPage))//如果cookie中的值和當(dāng)前頁相等,那么表示是刷新操作
{
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'為了保證您的訪問安全,頁面將在2秒后將自動(dòng)跳轉(zhuǎ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)文章
Asp.net利用JQuery彈出層加載數(shù)據(jù)代碼
最近看QQ空間里面的投票功能很使用。點(diǎn)擊一個(gè)鏈接就彈出一個(gè)層,然后再加載一些投票信息,旁邊的區(qū)域變成灰色不可用狀態(tài)。其實(shí)這不算什么高深的技術(shù),只要在ASP.NET中利用JQuery結(jié)合一般處理程序ASHX即可搞定了。2009-11-11
高效的.Net UDP異步編程實(shí)現(xiàn)分析
重點(diǎn)是怎么建立一種高效的UDP機(jī)制來實(shí)時(shí)接收服務(wù)器發(fā)送過來的數(shù)據(jù)包.本文將介紹.Net UDP異步編程如何實(shí)現(xiàn)解決方案,有需求的朋友可以參考下2012-11-11
Abp集成HangFire開源.NET任務(wù)調(diào)度框架
這篇文章主要為大家介紹了Abp集成HangFire開源.NET任務(wù)調(diào)度框架的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼
asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼,需要的朋友可以參考下2012-06-06
.Net學(xué)習(xí)筆記之Layui多圖片上傳功能
這篇文章主要給大家介紹了關(guān)于.Net學(xué)習(xí)筆記之Layui多圖片上傳功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.Net具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
.net decimal保留指定的小數(shù)位數(shù)(不四舍五入)
大家都知道decimal保留指定位數(shù)小數(shù)的時(shí)候,.NET自帶的方法都是四舍五入的。那么如何讓decimal保留指定位數(shù)小數(shù)的時(shí)候不四舍五入呢,下面通過這篇文中的示例代碼來一起看看吧。2016-12-12
設(shè)置默認(rèn)Ajax操作cache and error
設(shè)置默認(rèn)Ajax操作cache and error,需要的朋友可以參考一下2013-02-02

