ASP.NET website management system exit clear browser cache Session code

  • 2020-05-17 05:12:06
  • OfStack

1. Record the login user name, password and other information (part of the login code) when the system login is successful.
 
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 value of session is empty when the page is loaded
 
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 session clean code and browser cache clean code in the event of clicking "exit system"
 
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(); 
} 

Due to my "exit system" using HTML < a > The label is written in the master page, so the code above is written in the master page's.cs file.
Master page code:
 
<a class="atop" target="_self" <SPAN style="BACKGROUND-COLOR: #ff0000">runat="server" onserverclick</SPAN> ="Clear" > Log out </a> 

================================================================================================
The previous version 1 could not realize the function, and I struggled for a long time, but I did not find out the problem. I posted the code that was wrong at the beginning. At the same time, I also posted what I thought was wrong.
This version of the error is: successfully login into the main page, and then click to enter other pages can not enter, will jump to the login interface.
My thoughts:
1. When I was tracking debugging, I found that the master page was automatically executed every time the page was loaded. Therefore, if (Session["id"] == null ["name"] == null || Session["name"] == null || Session["authority"] == null || Session["pwd"] == null) Response. Redirect (" ~ / Login. aspx true);
2. My question is why the Clear() method is automatically executed every time the page is loaded when it is clicked.
3. The reason for my mistake is the difference in execution methods between the client side and the server side. Then I looked for the difference between onclick and onserverclick on the Internet, but my understanding of them was not very clear. I hope you can communicate with each other.
On the onclick, and the difference between onserverclick see: / / www ofstack. com article / 30313. htm
At the beginning, the foreground code is used (master page foreground code) :
 
<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
 
public void Clear() 
{ 
Session["id"] = null; 
Session["name"] = null; 
} 

Related articles: