简体   繁体   中英

Logout not working on browser close, with multiple tab opened

I am doing a SAML based authentication and want my user to be logged out of the system in case he closes the browser: I have written the following browser side code:

window.onbeforeunload =function(e){

deleteAllCookies();

var my_form=document.createElement('FORM');
my_form.name='my_form';
my_form.method='POST';
my_form.action="sessionExpired.do";

var hiddenText=document.createElement('INPUT');
hiddenText.type='HIDDEN';
hiddenText.name='crash';
hiddenText.ID='crash';
hiddenText.value='yes';
my_form.appendChild(hiddenText);

my_form.submit();

return;


};

This code calls my logout service and able to logg out of the system, in case one tab is opened, but the same doesn't work if along with my application multiple other tabs are opened.ie in case of multiple tab, my server code gets called which invalidates session, but still session remains logged in and is not asked for authentication again.

There are limits to what you can do in a handler for beforeunload .

You can return text to prompt the user to ask if they're sure they want to leave. But, you cannot force the window to stay open long enough to make the request to the server.

  • If you don't return a string, then the window will close immediately, and your form submission will not get processed.
  • If you do return a string, then the user will be asked if they're sure they want to close the window. If yes, the window will be closed, and your form submission will not get processed. If no, then the window will not be closed.

Deleting the cookies will probably work, but I don't think it will be possible to submit the form, or make a request to the server when the window is closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM