Tuesday, September 18, 2007

Session.Abandon() : On browser is closed

Some times it is necessary to block the access of multiple users to same information. A route to do this is to register the entrance and the exit of the user to the application (DB, Application Object). It happens then that if the user closes browser without closing session in the application for some reason the Session_End does not happen. An alternative is to use the ScriptManager (MS AJAX) and a pair of lines of Javascript to notify the servant when it is closed in browser.

In summary

  1. Subscribe to the OnUnload event of the Body (<body OnUnload="Logout (); ">)
  2. Invoke the Static WebMethod (it will close session) from the function Javascript (PageMethods.AbandonSession ();)
  3. As when sailing to another page also it invokes the OnUnload event, it is necessary to make a last adjustment. All the pages of the application will be sailed from a IFRAME from a Content.aspx page. The trick is, the IFRAME makes navigation to other pages whereas Content.aspx this statics.

I read these articles before:
http://aspalliance.com/520
http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide
http://www.codeproject.com/csharp/Detect_closing_navigator.asp
http://geeks.ms/blogs/lruiz/archive/2007/02/27/controlar-cuando-el-usuario-nos-cierra-el-navegador-es-posible.aspx
http://www.programmersheaven.com/mb/ASPNET/303808/303808/readmessage.aspx

It works.

No comments:

Google