0

There was a requirement in my project to notify the user of session expiry before it expires. I have implemented this in jquery using setTimeout. Its working fine till user is working in a single tab. The problem arises if multiple tabs are kept opened. In this case, even though user is working on a tab since other tabs are inactive alert is being displayed on those inactive tabs. This should not happen because user is active on one tab.

I am using something like below code:

    function SessionExpireAlert() {

        var timeout = 20000;

        var seconds = timer / 1000;


        document.getElementsByName("seconds").innerHTML = seconds;
        setInterval(function () {
            seconds--;
            document.getElementById("seconds").innerHTML = seconds;

        },1000);
        setTimeout(function () {
            //Show Popup before 20 seconds of timeout.
            $find("mpeTimeout").show();
        }, timeout - 19 * 1000);
        setTimeout(function () {

            window.location = "Default.aspx";
        }, timeout);
    };
    function ResetSession() {
        //Redirect to refresh Session.
        window.location = window.location.href;
    }
    function ResetTimers() {
        clearTimeout(seconds);
        SessionExpireAlert
                   }

I will be happy for any suggestion in this...

ManojK
  • 21
  • 3
  • So, the alert should show up only in the active tab? What should happen with the other inactive tabs? – Catalyst Jun 17 '17 at 07:22
  • yes, since user is active on a tab other tabs should not show the alert. And if user is inactive on all the tabs then the alert should be shown on all the tab with same timing in a synchronous manner. – ManojK Jun 17 '17 at 12:34

1 Answers1

0

In my opinion, if the user sessions are kept by the application server, is not a good practice set this type of control in the client-side code (Because is very hard to control and to keep synched with the server side like asp.net session timestamp).

Would be better create a web API or, alternatively sync by a library like signalR, to retrieve the current user informations. Here, will be the session timeout value.

Every time the user change the site location, or every time you want, you can call this API and retrieve the value and display this on the page as a timer for example. Of course, you can also force to reset, by client, the session timestamp though a api call.

Matteo Tosato
  • 185
  • 3
  • 15
  • Thanks for the suggestion but could you please provide me with some sample links. – ManojK Jun 17 '17 at 12:36
  • If your are using WEB forms with code behind, You can use the 'session' variables (Of course, you have to implement a base asp.net authentication). There are many similar discussions, for example the following: https://stackoverflow.com/a/20236481/5233057 You should refer to the 'sessionState' documentation to retrieve the timeout value: https://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.100).aspx – Matteo Tosato Jun 18 '17 at 13:55