Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a login form and some more forms for the process. I need to log off automatically when the user refresh or press back button or closes the browser how to perform this? Thanks in advance.
Posted

Hi
I gases you are in ASP.NET plat form so you can use timeout tag from authentication element follow of code demonstrate :

XML
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="3000">
</forms>
</authentication>


So maybe your application is hosted in IIS don`t worry we have a other solution for it please follow these steps:

1) Please open your IIS with this command "inetmgr" in run
2) Expand Default Web site and Right Click on your Web Site/Proerties
3) In the Web Site Properties dialog box go to ASP.Net tab click on Edit Configuration button
4) In the ASP.Net Configuration Setting dialog box go to State Management Tab and Increase the Session timeout (minutes)

Of course you should use FormsAuthentication.SignOut you can get more information about it here: http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.signout.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2.

C#
public void Login_OnClick(object sender, EventArgs args)
{
  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage();
}


So you can use these method for sign out when the user refresh the page ;)
Best Regards.
 
Share this answer
 
Its bed idea.What if the page is not properly loaded and user wants to go back or refresh the page? Go through this link which describes this.You can further implement it using this link
 
Share this answer
 
v2
Comments
Member 9762654 2-May-13 1:23am    
I have displayed menu according to the username so after logging in if a user press back button the user name which i stored in session will be empty so if again the user press forward button my menu displays all values which should not happen
Thanks7872 2-May-13 1:24am    
I think you want such that user should be able to access the page only if he provides his/her credentials,right?
Member 9762654 2-May-13 1:37am    
yes
Thanks7872 2-May-13 1:38am    
Put <script language="JavaScript" type="text/javascript">
javascript: window.history.forward(1);
</script>
this code in head section of your login page.thats it.you are done.Accept or upvote the answer if it helped you.
These ways won't be really reliable. When you press the back button, you get a page from cache, so your server side does not get an HTTP request. Also, when you just close the browser window, nothing happens; however, this case is easier: you can handle this event in JavaScript.

Even though you cannot handle a click the "Back" navigation operation, you can instead disable back button is certain cases. Please see this CodeProject article: Browser back button issue after logout[^].

Now, about capturing the even when a user closes a browser window.

You can handle the events onunload in JavaScript:
http://www.w3schools.com/jsref/event_onunload.asp[^].

In the handler, you can use Ajax to send an HTTP request for log off.

—SA
 
Share this answer
 
Comments
Member 9762654 2-May-13 1:36am    
onunload will occur even when the user clicks a link or submits a form that should not happen only when the user closes the browser i have to log off
Sergey Alexandrovich Kryukov 2-May-13 9:35am    
Agree. This is not as simple. See also:
https://www.outsystems.com/forums/discussion/8513/using-the-onunload-javascript-function-to-force-a-log-off-of-app
http://www.codeproject.com/Questions/98141/Force-log-off-on-browser-exit
http://manicode.blogspot.com/2007/12/logout-via-javascript-with-onunload.html
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900