Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logoff User when browser tab page is closed, ASP.NET MVC

In one of the ASP.NET MVC apps we would like to logoff the user automatically if he closes the browser tab in which the app is opened.

We are using the following code when he authenticates.

FormsAuthentication.SetAuthCookie(userName, false)

As of now, if we closes the browser window and relaunch it, users are asked to authenticate again. But we want to ask users to authenticate again if they close the tab and try to access any of the website urls.

like image 403
Gopinath Avatar asked Sep 02 '09 10:09

Gopinath


3 Answers

We decided to use cookie less authentication so that the authentication token is part of the url. When the tab is closed and they open the website again, they will be asked to authenticate again :)

like image 101
Gopinath Avatar answered Nov 16 '22 03:11

Gopinath


I have not tried this myself, but I think the following approach should work:

On the client side, you can use the OnUnload event of your document to launch a javascript function that would call your server-side signout method via ajax.

On the server side, you should have the action method call FormsAuthentication.SignOut() and Session.Abandon();

like image 3
Adrian Grigore Avatar answered Nov 16 '22 01:11

Adrian Grigore


A browser clears all Session scoped objects only when it is completely closed, and not when an individual tab is closed.

One way could be to use a very low Session timeout and have a server-side script poll every few seconds to hit an object on the page. This will extend Session time again. So if a tab is closed, the script can't find the object thereby allowing the Session to timeout. One problem here is if your app is on a pretty high load, your app could DoS itself!

like image 3
Druid Avatar answered Nov 16 '22 03:11

Druid