Question browser closed time session clear

selimsoydann

Member
Joined
Jan 1, 2021
Messages
5
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:
Looks like JavaScript to me. I'm not understanding how this is a C# question.
 
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();
 
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.
 
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.
 
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.
 
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.
 
Back
Top Bottom