标签/浏览器关闭时如何注销用户?

时间:2019-03-27 11:52:22

标签: javascript session logout onbeforeunload

在MVC应用程序中关闭标签页/浏览器时,在body标记中的beforeunload上应用注销用户。

我设法以某种方式绕过了重定向到其他链接以及F5的操作,但是当我单击“刷新”按钮时,它使我注销,而且当我在地址栏中选择url并单击Enter时,它也将我注销。




     <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>

如何绕过所有其他事件,例如表单提交,锚定标记单击,按钮按下输入提交,以触发此事件,或者至少可以检查是否单击/单击/提交了上述任何事件。

0 个答案:

没有答案