asp.net website anti-malicious refreshing cookies and Session solution _ practical Tips

Source: Internet
Author: User

This article is an example of the ASP.net Web site to prevent malicious refreshing cookies and session solution, is a web design very practical skills. Share to everyone for your reference. The implementation methods are as follows:

Session Version Implementation method:

public double time;  public const int freetime = 1;//Brush freeze interval, currently 1 seconds #region anti-malicious refresh 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 *) {warm_prompt (); #endregion public void Warm_prompt () {Response.Write ("<table width= ' 778 ' border= ' 0 ' align= ' center ' Cellpaddin g= ' 3 ' cellspacing= ' 2 ' bgcolor= ' #009900 ' style= ' font-size:14px;
    ' > ');
    Response.Write ("<tr bgcolor= ' #FFFFFF ' >");
    Response.Write ("<td></td>"); Response.Write ("<td bgcolor= ' #EEFFEE ' to ensure your access security, please" +FreeTime + "seconds after <a href= '" + Request.rawurl + "' target= ' _self ' style= ' color: #FF0000; '
    > Click here to refresh </a> this page </td> ");
    Response.Write ("</tr>");
    Response.Write ("</table>");
Response.End ();

 }

Cookie Implementation method:

public double time;

public const int freetime = 2;
#region anti-malicious refresh of the string page;
if (request.cookies["page" = = null) {page = "";} else {page = httpcontext.current.request.cookies["page"]. Value.tostring (); Gets the URL value stored in the cookie} string strthispage = HttpContext.Current.Request.Url.PathAndQuery.ToString ();//Get current page address DateTime
Lasttime = DateTime.Now; if (page. Equals (Strthispage))//If the value in the cookie is equal to the current page, the expression is the refresh operation {TimeSpan ts = lasttime-datetime.parse ( httpcontext.current.request.cookies["Time"].

  Value.tostring ()); Time = ts.
  Seconds;
  if (Time < FreeTime) {warm_prompt (); } else {//Execute Action 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 ') to ensure your Access security, the page will automatically jump to the content you want to visit in 2 seconds!
    </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 is believed that this article has certain reference value to the ASP.net program design of our people.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.