Saravanan
Saravanan

Reputation: 11592

Is this possible to clear the session whenever browser closed in asp.net?

In my asp.net application, i want to clear the session whenever my browser closed or my tab (if my browser containing multiple tabs)closed.

Please guide me to get out of this issue...

Upvotes: 7

Views: 54865

Answers (7)

Praveenkumar.s
Praveenkumar.s

Reputation: 1

Yes.First of all Browser automatically clear session when browser is closed. you can try to capture browser close or tab close event in browser using javascript function like on before unload and on unload. Mostly onbefore unload event captures browser close event in chrome, Firefox, IE 11.

Upvotes: -1

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

I think cookies can better meet your requirement here for session management.

it means that session data should not be stored on the server and should be with your call, so that you don't have to worry about clearing the data on server.

Upvotes: 0

Shazhad Ilyas
Shazhad Ilyas

Reputation: 1193

Alternatively you can check you previous session state on every new browser opening and can Session.clear() or Session.abandon() the previous session.

this will make sure that every time you start application you will get new session.

  1. use BasePage in your .net application.
  2. Check the session.sessionid on basepage load.
  3. More Inforamtion how to detect new session in basepage. BasePage.Session.Link

Hope this helps regards Shaz

public class BasePage : Page
{

            protected string mySessionId;   

            private CurrentUser _currentUser;
            public CurrentUser _CurrentUser
            {
                get { return ((CurrentUser)HttpContext.Current.Session["myCurrentUser"]); }
                set { _currentUser = value; }
            }


            protected override void OnLoad(EventArgs e)
            {

                if (Session["myCurrentUser"] != null)
                {
                    if (_CurrentUser.ProUser)
                    {

                       mySessionId = Session.SessionID; // it means New Session

                    }

                    if (!mySessionId.IsNullOrDefault() && mySessionId != Session.SessionID)
                    {
                        Session.Abandon(); //Abandon current session and start new one                

                    }
                }


            }

}

Upvotes: 1

iShare
iShare

Reputation: 125

You can use Session_End event of Global.aspx

//For Specific Session
Session.Remove("SessionName");

//All the Session
Session.Abandon();

Upvotes: -2

PaulMolloy
PaulMolloy

Reputation: 632

Short version, No. There's no solid way of a server detecting if the client has closed their browser. It's just the nature of web development's asynchronous pattern.

Long version, if it's really, really important to you; Put a bit of javascript in the page that sends a regular post to your website in the background and set up a serverside agent or service that disposes of the sessions if it doesnt receive these regular "heartbeat" signals.

You can put a javascript postback onto the page's unload() event but dont rely on it, it doesnt always fire.

Upvotes: 11

Lawrence Johnson
Lawrence Johnson

Reputation: 4043

This happens by default whenever you close your browser, and that's not just for ASP.NET. It's for most server-side programming languages that have a session state. Basically, any cookie that is added that doesn't specify an expiration date, will be deleted when the browser is closed.

Where this doesn't apply, is when you close a tab, which is something you will not have any control over because the tab close event will not get sent back to the Web server.

Upvotes: 2

Jordi
Jordi

Reputation: 2797

You can try to do that with javascript. Check it at:

http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed

Upvotes: 1

Related Questions