About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

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

© Copyright 2024

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 (22) | 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 4, 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 5, 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 5, 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 6, 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 9, 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.

Priya United States

Friday, July 29, 2011 9:29 AM

Priya

I am using this in MAster page and it is not working in any browser.Can you please help me what I am doing wrong
Thanks
Priya

cnr Turkey

Thursday, February 2, 2012 2:39 PM

cnr

how can i download runing project?

Mahesh kumar India

Sunday, March 11, 2012 6:36 AM

Mahesh kumar

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.

and
i want know that it will work for all browsers.

Vinod India

Tuesday, August 7, 2012 5:00 AM

Vinod

Hi...

please provide me source code for session.abandon()  after browser close.

Above code is not working for me.

Thx in Adv.

Murali India

Tuesday, December 11, 2012 4:04 AM

Murali

if ((window.event.clientX < 0) || (window.event.clientY < 0) || window.event.clientY < -80)
In the above line, i am not getting any IntelliSense after typing window.
Please help me out from this.

Thanks in Advance.....

Murali India

Tuesday, December 11, 2012 4:17 AM

Murali

if ((window.event.clientX < 0) || (window.event.clientY < 0) || window.event.clientY < -80)
In the above line, i am not getting any IntelliSense after typing window.
Please help me out from this.

Thanks in Advance.....

siteduct.com

Thursday, January 3, 2013 8:10 AM

pingback

Pingback from siteduct.com

ASP Script Installation Service

Ashok Luhach India

Thursday, March 21, 2013 4:28 AM

Ashok Luhach

I am using this in MAster page and it is not working in any browser.Can you please help me what I am doing wrong

Thanks

Sudheer Kumar India

Sunday, April 21, 2013 6:56 AM

Sudheer Kumar

How can i get logout time while closing browser tab please help me

Dinesh India

Thursday, May 23, 2013 7:01 AM

Dinesh

The above code not working when i press [X] button of browser and press Alt+F4.


Please help me.

Dinesh India

Thursday, May 23, 2013 7:02 AM

Dinesh

The above code not working when i press [X] button of browser and press Alt+F4.


Please help me.

alexatrafficracer.org

Sunday, September 15, 2013 9:52 AM

pingback

Pingback from alexatrafficracer.org

Alexa Traffic Racer

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading