Click here to Skip to main content
12,954,856 members (75,856 online)
Rate this:
Please Sign up or sign in to vote.
See more:
dear all

how can i clear session when browser is closed using and c#
Posted 27-Jun-11 23:50pm
BobJanova 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 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
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