Applied onbeforeunload in body tag to logout user when a tab/browser is closed in an MVC Application.
I somehow managed to bypass redirecting to other links and also F5, but when I click on refresh button it logs me out, also when I select url in address bar and hit enter it logs me out.
<body onbeforeunload="UserLogoutOnBrowserClose(event)">
<script>
var href;
var id;
var logout;
var descargar;
var isF5Pressed;
$('a').click(function () {
href = ($(this).attr('href'));
id = ($(this).attr('id'));
if (id === "logout")
logout = id;
else if (id === "descargar")
descargar = id;
});
$(document.body).on("keydown", this, function (event) {
if (event.keyCode == 116) {
isF5Pressed = true;
}
});
$(window).on("unload", this, function (event) {
console.log(window.performance);
});
function UserLogoutOnBrowserClose(e) {
debugger;
var navigation = e.currentTarget.performance.navigation.type;
var activeElement = document.activeElement;
var myWindow;
var Uri = '@Url.Action("LogOff", "Account",new { area="" })';
var bodyID = '@bodyId';
var attributes = document.activeElement.attributes;
var attributesId = (document.activeElement.attributes).id;
var baseURI = document.baseURI;
// submit-pse-button check is applied to prevent user from signing out while redirecting to PSE portal
if (attributesId.value === "submit-pse-button" || attributesId.value === "reclamarButton" || activeElement.localName === "a"
|| activeElement.localName === "button" || logout != undefined || descargar != undefined || isF5Pressed != undefined) { }
else {
if (attributes.href != undefined) {
if (bodyID === "customer" && baseURI == attributes.href.value)
myWindow = window.open(Uri, "");
}
else {
if (bodyID === "customer")
myWindow = window.open(Uri, "");
}
}
}
</script>
How to bypass all other events like form submits, anchor tag clicking, button pressing input submits to have this event fired or at least can check if any of the above is clicked/hit/submitted.