About the author

Vijay Kodali
E-mail me Send mail

Site Statistics

Site Meter

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Asp.net session on browser close

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.


Posted by vijay on Thursday, April 29, 2010 6:09 PM
Permalink | Comments (15) | Post RSSRSS comment feed

Comments

Vijay Kodali's Blog

Thursday, April 29, 2010 7:20 PM

trackback

Asp.net session on browser close

Note: Cross posted from Vijay Kodali's Blog . Permalink How to capture logoff time when user closes browser

dotnetguts United States

Tuesday, May 04, 2010 11:43 AM

dotnetguts

Hi Vijay,

That was really good work around, Is there any way we can avoid using Script Manager?

vijay United States

Wednesday, May 05, 2010 8:30 AM

vijay

@dotnetguts
No, To enable an .asmx Web service to be called from client script in an ASP.NET Web page, you must add a ScriptManager control to the page. why do you want to avoid it in first place?

dotnetguts United States

Wednesday, May 05, 2010 9:37 PM

dotnetguts

Reason i want to avoid Script Manager is Ajax is making Page size bigger.  If it was limit to one page than it was ok, but as you never know, that from which page user closes browser window, so I believe i need to put that in master page, which is not advisable.

Also if possible could you please explain your client-side function in little detail.

Specially line: WebApplication1C.AsynchronousSave.SaveLogofftime...

What is WebApplication1C here?  App name, assmebly name...?

vijay United States

Thursday, May 06, 2010 8:56 PM

vijay

@dotnetguts

WebApplication1C- Application Name
AsynchronousSave - Webservice Name
SaveLogofftime - Method name in webservice

Regarding script manager, Yes it will increase page size slightly.

Manik United States

Thursday, May 13, 2010 2:20 PM

Manik

This script is cool and works on IE. However it doesnt work on FireFox and many other browsers.
The main reason is window.event.clientX is undefined in firefox unless a client side event occurs on a page control e.g. button etc.
May be you have a solution for this?

Ujjwala Datta India

Wednesday, May 19, 2010 9:38 PM

Ujjwala Datta

Nice Article, Thank you.

vijay United States

Thursday, May 20, 2010 9:45 PM

vijay

@ManiK,
Yes, window.event doesnt work with browsers other than IE.  But in most browsers window.onbeforeunload will be called everytime you navigate away from the page.

Just comment out that if condition line, you should be fine.

Satish India

Wednesday, June 09, 2010 2:12 AM

Satish

Hi Vijay,

it's very nice article.

but it will not work on multiple tab in IE7.

i.e. In IE 7 there are multiple tab. on one tab i have open my application and another tab there is google.com. when i am trying to close browser this article doen't work.

I hope you have solutions for the above same.
thanks
Satish

vijay United States

Friday, June 18, 2010 8:23 AM

vijay

@Satish,
Yes, this solution won't for IE7/8 multi tabs. But if you wish to override this behavior and get a new session, you can chose “File – New Session” from IE menu.

miami seo company United States

Saturday, August 28, 2010 12:04 AM

miami seo company

Local Look for engine is the way in which on the long term. Google is making searches come up with more local final results such as the local company final results and Google Maps. The other major seek engines are doing a similar. It will not be long till it becomes the sole method to find organizations. Here is exactly where local motor devices assist enterprises to obtain spotted.

Tom Mordehay United States

Saturday, August 28, 2010 10:15 AM

Tom Mordehay

i thought this was a really great post to read.  i'll check back for new posts by you!

best movie quotes United States

Saturday, August 28, 2010 12:39 PM

best movie quotes

to be honest, some of the people i know, still quote alot of older and more famous movies, sometimes it does get annoying but you know, i eitger get used to it or i ignore them.

Wes Nouth United States

Tuesday, August 31, 2010 3:25 AM

Wes Nouth

i can't wait for taylor lautner's new movie abduction to come out...he's so awesome!

Vance Mckillop United States

Wednesday, September 08, 2010 5:08 PM

Vance Mckillop

Should you be looking to set up your own private internet business by tapping the possibility that the World wide web has to offer then it is just a move in the best place. On the other hand, the most important headache just isn't to shift each of the advertising pursuits of your business enterprise to others. When getting top notch website traffic and site visitors who are leads, it is important to groundwork and pay attention to the right strategies toward promoting your business enterprise over the internet. If you achieve necessary knowledge then it'll make sense in outsourcing the marketing work connected with your home business to others.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading