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;
}