Asp.net session on browser close

Note: Cross posted from Vijay Kodali's Blog.
Permalink

How to capture logoff time when user closes browser?

Or

How to end user session when browser closed?

These are some of the frequently asked questions in asp.net forums.

In this post I'll show you how to do this when you're building an ASP.NET web application.

Before we start, one fact:

There is no full-proof technique to catch the browser close event for 100% of time. The trouble lies in the stateless nature of HTTP. The Web server is out of the picture as soon as it finishes sending the page content to the client. After that, all you can rely on is a client side script. Unfortunately, there is no reliable client side event for browser close.

Solution:

The first thing you need to do is create the web service. I've added web service and named it AsynchronousSave.asmx. 

 Open Dialog

Make this web service accessible from Script, by setting class qualified with the ScriptServiceAttribute attribute... 

clip_image004

Add a method (SaveLogOffTime) marked with [WebMethod] attribute. This method simply accepts UserId as a string variable and writes that value and logoff time to text file. But you can pass as many variables as required. You can then use this information for many purposes.

clip_image006

To end user session, you can just call Session.Abandon() in the above web method.

To enable web service to be called from page’s client side code, add script manager to page. Here i am adding to SessionTest.aspx page

clip_image008

When the user closes the browser, onbeforeunload event fires on the client side. Our final step is adding a java script function to that event, which makes web service calls. The code is simple but effective

clip_image010

My Code

HTML:( SessionTest.aspx )

clip_image012

C#:( SessionTest.aspx.cs )

clip_image014

That’s’ it. Run the application and after browser close, open the text file to see the log off time.

clip_image016

The above code works well in IE 7/8. If you have any questions, leave a comment.

Published Thursday, April 29, 2010 9:40 PM by budugu
Filed under: , , ,

Comments

# re: Asp.net session on browser close

Friday, April 30, 2010 12:26 AM by Arun Mahendrakar

That looks interesting; thanks Vijay.

Arun

# Twitter Trackbacks for Asp.net session on browser close - Vijay Kodali's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Asp.net session on browser close - Vijay Kodali's Blog         [asp.net]        on Topsy.com

# re: Asp.net session on browser close

Monday, May 10, 2010 6:24 PM by Roberto

Interesting article.  Thanks for sharing!

# re: Asp.net session on browser close

Monday, May 24, 2010 2:52 AM by vijaya.a

Hi vijay thanks for sharing nice article.will it work in ie6?

# re: Asp.net session on browser close

Monday, May 24, 2010 6:12 PM by budugu

@Vijaya.a,

Not sure. I didn't test this in IE6.

# re: Asp.net session on browser close

Tuesday, August 24, 2010 5:07 AM by Zeeshan Umar

Interesting article, keep posting like that. Thanks for sharing.

# re: Asp.net session on browser close

Monday, November 15, 2010 7:25 AM by anurag

very nice article Vijay. Thnx a ton for sharing.

# re: Asp.net session on browser close

Thursday, March 10, 2011 7:41 AM by chris

I am using VB.net in the code behind, the code does not work for me I am trying to save the log out Time I mean TimeOfDay() in the Database when the user close the browser

I really need help

Thanks

# re: Asp.net session on browser close

Wednesday, April 13, 2011 7:06 AM by Mohit Sharma

Hey Vijay, its a great code, although it doesn't work with IE6... I tried it with a seperate code, it worked well but it rewrites the txt file everytime i run the website n close the browser... could something be done regarding if i want it to keep writing the logoff info in the same document on different lines...

# re: Asp.net session on browser close

Monday, June 13, 2011 4:13 PM by Bestin

Really good article. Good work. Helped me a lots.

# re: Asp.net session on browser close

Friday, July 29, 2011 11:32 AM by Priya

i am using your code on master page.But It does not seem it is working in any brwser.Can you tell me if it will work on master pages.

Thanks

Priya