Question browser closed time session clear

selimsoydann

New member
Joined
Jan 1, 2021
Messages
4
Programming Experience
1-3
hello I have a code like the one below. Here I want the session cleared as soon as the browser is closed. But when I close the browser, the script works, it goes to function, it runs my code. I am still logged in when I open the browser again. Can you help me.

JavaScript:
<script type="text/javascript">       
    $(document).ready(function () {       
        window.addEventListener('beforeunload',recordeCloseTime);   
    });   
    function recordeCloseTime() {       
        $.ajax({
            type: "POST",       
            url: "Account/LogOut",   
        });       
    }
</script>
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
Looks like JavaScript to me. I'm not understanding how this is a C# question.
 

selimsoydann

New member
Joined
Jan 1, 2021
Messages
4
Programming Experience
1-3
Looks like JavaScript to me. I'm not understanding how this is a C# question.
this is javascript yes I post it as a controller in mvc when I close the browser. but when I go to the controls I want, the session does not clean one round, only when I close the browser.

Controller
Session.Abandon();
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
Set a breakpoint on your Session_OnEnd() handler. (Add one temporarily if needed.) My understanding is that a session is not really dead until after the session has ended. Calling Session.Abandon() just marks the session for deletion.

You could also just try the brute force Session.Clear() followed by calling Session.Abandon(), but personally, I'd rather narrow down first if the session is even ending.
 

selimsoydann

New member
Joined
Jan 1, 2021
Messages
4
Programming Experience
1-3
Set a breakpoint on your Session_OnEnd() handler. (Add one temporarily if needed.) My understanding is that a session is not really dead until after the session has ended. Calling Session.Abandon() just marks the session for deletion.

You could also just try the brute force Session.Clear() followed by calling Session.Abandon(), but personally, I'd rather narrow down first if the session is even ending.
Thanks for your hello reply. I understand that when I close the browser, I need to trigger the session end event in global asax cs. I have no idea how to do this in mvc. Can you give me an idea.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
You misunderstood. I'm not asking you to trigger the session end. You are already trying to end the session by calling Session.Abandon(). I'm trying to get you to determine if the session truly is ending by setting a breakpoint in the event handler for the end of a session to confirm that the session that you think has ended truly has ended.
 

selimsoydann

New member
Joined
Jan 1, 2021
Messages
4
Programming Experience
1-3
You misunderstood. I'm not asking you to trigger the session end. You are already trying to end the session by calling Session.Abandon(). I'm trying to get you to determine if the session truly is ending by setting a breakpoint in the event handler for the end of a session to confirm that the session that you think has ended truly has ended.
Hello again, thank you. I put a break point in the event handler, but when I close the browser, the controller comes, but global asax does not come to the session end event. Could you please give me some more information? I really need it. What should I do? What I want to do is actually clean the session completely when the browser is closed.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
I don't know. But it looks like there some interesting answers here:
 
Top Bottom