Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#3.0 ASP.NET
dear all
how can i clear session when browser is closed using and c#
Posted 27-Jun-11 23:50pm
BobJanova at 28-Jun-11 6:27am
You CANNOT reliably detect this. Do not rely on any of the JS-related approaches. If the user's network connection drops or the process is killed, you will never get a notification.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Please refer the below steps.

1. First create a page SessionClear.aspx and write the code to clear session

2. Then add following JavaScript code in your page or Master Page:-
<script language="javascript" type="text/javascript">
    var isClose = false;
    //this code will handle the F5 or Ctrl+F5 key
    //need to handle more cases like ctrl+R whose codes are not listed here
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
    var keycode;
    if (window.event)
    keycode = window.event.keyCode;
    else if (e)
    keycode = e.which;
    if(keycode == 116)
    isClose = true;
    function somefunction()
    isClose = true;

        function bodyUnload() {
            	var request = GetRequest();
   "GET", "SessionClear.aspx", true);
        function GetRequest() {
            var request = null;
            if (window.XMLHttpRequest) {
                //incase of IE7,FF, Opera and Safari browser
                request = new XMLHttpRequest();
            else {
                //for old browser like IE 6.x and IE 5.x
                request = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            return request;

3.  Add the following code in the body tag of master page.

<body onbeforeunload="bodyUnload();" onmousedown="somefunction()">
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can two function,
Session.Abandon() destroys the session and the Session_OnEnd event is triggered. if you use Session.Abandon(), you lose that specific session and the user will get a new session key. You could use it for example when the user logs out.
Session.Clear(), if you want that the user remaining in the same session and reset all his session specific data
You can study the following link,[^][^]
Toniyo Jackson at 28-Jun-11 6:07am
He wanted to clear the session when browser closed!!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See answer in the discussion @[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 202
2 Richard MacCutchan 160
3 PhilLenoir 154
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 5,999
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505

Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100