ASP. NET Website management system exit to clear browser cache, Session code

Source: Internet
Author: User
Tags clear browser cache

1. Record the login username and password when the system logs in successfully (some codes of the login function)
Copy codeThe Code is as follows:
Session ["id"] = user. id. ToString ();
Session ["name"] = user. name. ToString ();
Session ["pwd"] = user. password. ToString ();
Session ["time"] = user. LoginTime. ToString ();
Session ["authority"] = user. limits. ToString ();

2. Add the following code to each page of the management system to determine whether the session value is blank during page loading.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (Session ["id"] = null | Session ["name"] = null | Session ["time"] = null | Session ["authority"] = null | Session ["pwd"] = null)
Response. Redirect ("~ /Login. aspx ", true );
If (! IsPostBack)
{
......
}
}

3. Add the session clearing code and the code cached by the browser to the "Exit System" execution event.
Copy codeThe Code is as follows:
Public void Clear (object sender, EventArgs e)
{
Session ["id"] = null;
Session ["name"] = null;
ClearClientPageCache ();
Response. Redirect ("~ /Login. aspx ");
}
Public void ClearClientPageCache ()
{
// Clear the browser cache
Response. Buffer = true;
Response. ExpiresAbsolute = DateTime. Now. AddDays (-1 );
Response. Cache. SetExpires (DateTime. Now. AddDays (-1 ));
Response. Expires = 0;
Response. CacheControl = "no-cache ";
Response. Cache. SetNoStore ();
}

Because the HTML <a> label is used to write my "Exit System" in the master page, the above Code is written in the. cs file on the master page.
Master Page code:
Copy codeThe Code is as follows:
<A class = "atop" target = "_ self" <SPAN style = "BACKGROUND-COLOR: # ff0000 "> runat =" server "onserverclick </SPAN> =" Clear "> exit system </a>

========================================================== ========================================================== ======================
The previous version has been unable to implement the function. It has been entangled for a long time and has not found out the problem. I have posted the code for executing the error at the beginning, and I have also posted the code that I think is wrong, I hope everyone will criticize and correct it.
The error of this version is: after login is successful, you can enter the main page, and then click to enter other pages, you will not be able to enter, will jump to the login interface.
My thoughts:
1. During tracking and debugging, I found that the master page is automatically executed every time the page is loaded. the Clear () method in the cs file, so you cannot use Page_Load () on other pages () if (Session ["id"] = null | Session ["name"] = null | Session ["time"] = null | Session ["authority"] = null | Session ["pwd"] = null) response. redirect ("~ /Login. aspx ", true );
2. My question is, the Clear () method is executed only after being clicked. Why is it automatically executed every time the page is loaded?
3. The reason for my consideration is that the client and server have different execution methods. Then I found the difference between onclick and onserverclick on the Internet, but I still don't know much about them. I hope you can talk about it.
For the differences between onclick and onserverclick, see: http://www.jb51.net/article/30313.htm
At the beginning, the front-end code is used (the front-end code of the master page ):
Copy codeThe Code is as follows:
<A href = "~ /Login. aspx "class =" atop "target =" _ self "onclick =" clear () "> account information </a>
<Script>
Function clear () <BR >{< BR> <% Clear () ;%> <BR>}
</Script>

Master page background code
Copy codeThe Code is as follows:
Public void Clear ()
{
Session ["id"] = null;
Session ["name"] = null;
}

Related Article

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.