Session Timeout Warning in ASP.NET

<script type="text/javascript">
    var sessionTimeoutWarning = "<%= System.Configuration.ConfigurationManager.AppSettings["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";

    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
    setTimeout('SessionWarning()', sTimeout);

    function SessionWarning() {
        var message = "Your session will expire in another " +
            (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) +
            " mins! Please Save the data before the session expires";
        alert(message);
    }
</script>

This has been addressed before, e.g. ASP.NET - Javascript timeOut Warning based on sessionState timeOut in web.config

However, AFAIK there isn't a totally reliable way to do this, since:

  • If the user has more than one window open using the same session, then one window may be more recent than the other and the client session timeouts on the oldest window would be stale / incorrect.
  • If you round trip to the server to see what the current session expiration is, you will extend it, thus defeating the purpose of the popup / alert.