asp.net網站防惡意重新整理的Cookies與Session解決方案_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了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();  }}#endregionpublic 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();}#endregionpublic 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程式設計有一定的借鑒價值。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.