How to logout my application when I closed the window?

后端 未结 9 1933
猫巷女王i
猫巷女王i 2020-12-15 13:33

In my chat application i am having the logout button and it works fine.

Now I need to logout the application when I closed the browser window also..How can I achieve

9条回答
  •  太阳男子
    2020-12-15 13:44

    After lots of search I wrote the below customized code in javascript and server side code for session kill in c#.

    The below code is extended in case of same website is open in multiple tabs so the session is alive till one tab of website is open

    //Define global varible 
    var isCloseWindow = false;
    
     //Jquery page load function to register the events
    
     $(function () {
            //function for onbeforeuload
            window.onbeforeunload = ConfirmLeave;
            //function for onload
            window.onload = ConfirmEnter;
    
            //mouseover for div which spans the whole browser client area 
            $("div").on('mouseover', (function () {
            //for postback from the page make isCloseWindow global varible to false
                isCloseWindow = false;
            }));
            //mouseout event
            $("div").on('mouseout', (function () {
         //for event raised from tabclose,browserclose etc. the page make isCloseWindow global varible to false
                isCloseWindow = true;
            }));
        });
         //Key board events to track  the  browser tab or browser closed by ctrl+w or alt+f4 key combination
        $(document).keydown(function (e) {
            if (e.key.toUpperCase() == "CONTROL") {
                debugger;
                isCloseWindow = true;
            }
            else if (e.key.toUpperCase() == "ALT") {
                debugger;
                isCloseWindow = true;
            }
            else {
                debugger;
                isCloseWindow = false;
            }
        });
    
        function ConfirmEnter(event) {
            if (localStorage.getItem("IsPostBack") == null || localStorage.getItem("IsPostBack") == "N") {
                if (localStorage.getItem("tabCounter") == null || Number(localStorage.getItem("tabCounter")) == 0) {
                    //cookie is not present
                    localStorage.setItem('tabCounter', 1);
    
                } else {
                    localStorage.setItem('tabCounter', Number(localStorage.getItem('tabCounter')) + 1);
                }
            }
            localStorage.setItem("IsPostBack", "N");
        }
        function ConfirmLeave(event) {
            if (event.target.activeElement.innerText == "LOGOUT") {
                localStorage.setItem('tabCounter', 0);
                localStorage.setItem("IsPostBack", "N");
            } else {
                localStorage.setItem("IsPostBack", "Y");
            }
            if ((Number(localStorage.getItem('tabCounter')) == 1 && isCloseWindow == true)) {
                localStorage.setItem('tabCounter', 0);
                localStorage.setItem("IsPostBack", "N");
                **Call Web Method Kill_Session using jquery ajax call**
            } else if (Number(localStorage.getItem('tabCounter')) > 1 && isCloseWindow == true) {
                localStorage.setItem('tabCounter', Number(localStorage.getItem('tabCounter')) - 1);
            }
        }
    
     //C# server side WebMethod
     [WebMethod]
      public static void Kill_Session()
        {
            HttpContext.Current.Session.Abandon();
        }
    

提交回复
热议问题