asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法
來源:易賢網(wǎng) 閱讀:592 次 日期:2014-11-01 08:21:15
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法”,方便廣大網(wǎng)友查閱!

本文實例講述了asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法,是WEB程序設(shè)計中非常實用的技巧。分享給大家供大家參考。具體實現(xiàn)方法如下:

Session版實現(xiàn)方法:

public double time;

public const int freetime = 1;//防刷冰凍時間間隔,當(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'為了保證您的訪問安全,請您 " + freetime + " 秒后<a href='" + Request.RawUrl + "' target='_self' style='color:#FF0000;'>點擊這里刷新</a>此頁面</td>");

Response.Write(" </tr>");

Response.Write("</table>");

Response.End();

}

Cookies版實現(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中存儲的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秒后將自動跳轉(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();

}

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)