0

I'm trying to do auto redirect when session timeout using WebForms. I searched but found the codes that sets the predefined limit. but they never reset when we press key or move pointer etc. I reset the session timeout using ajax by calling ajax on each mouse move and keypress but this effecting the other script in terms of performance making it slower etc. I have tried this clear and clean code for this job also applied some ajax but never succeeded. https://code.msdn.microsoft.com/Auto-redirect-to-login-e1782b2f

Here is my service that get the session expire time on each mouse move or keypress.

    [WebMethod (EnableSession=true)]
    public static string GETExpireTime()
    {
        DateTime date = DateTime.Now;
        int sessionTimeout = HttpContext.Current.Session.Timeout;
        DateTime dateExpress = date.AddMinutes(sessionTimeout);
        return dateExpress.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");
    }

Here is how I'm resetting it in jquery.

$(document).ready(function () {
        $(document).keyup(function () {
            var data = {};
            setCookie("express", generalAjax(data, 'GETExpireTime').d);
        });
        $('*').mouseenter(function () {
            var data = {};
            setCookie("express", generalAjax(data, 'GETExpireTime').d);

        });
    });

Please help me if there is another way to do this perfectly as all codes set predefined time and force logout after that interval But I need to reset at mouse move or keypress. Thanks

  • Possible duplicate of [Redirect to specific page after session expires (MVC4)](https://stackoverflow.com/questions/25423464/redirect-to-specific-page-after-session-expires-mvc4) – TAHA SULTAN TEMURI Jun 23 '18 at 05:50

2 Answers2

0

Why do you use ajax for every keypress or mouse enter?

You can use interval function in jQuery to check is timeout or not. And if it time out, do every thing you need.

For start interval you just set a variable of start session time and an other variable duration.

I would it helps you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
masoumeh miran
  • 180
  • 1
  • 9
  • you are not giving solution please use comment for questions. – TAHA SULTAN TEMURI Jun 23 '18 at 05:51
  • If you use interval and use session variable inside then it never refresh the value. it just take the value when the page was loaded. you can try this experiment. – Muhammad Rashid Abbas Jun 23 '18 at 05:56
  • in backend you have 2 items starttime and duration when you want to know is expired you just subtract time.now - startTime if it was bigger than duration you know is expired , and in front end when page is load you have both variable and call function interval to check it. – masoumeh miran Jun 23 '18 at 06:28
0

As this solution can be done with JavaScript only. So I came up with my own code. That is based on counter. Counter is reset on keyup or mouse movement. also event is cleared and being registered back.

<script>
        var delay = 10000;
        var sTimeoutMinutes = "<%=Session.Timeout %>";
        var sTimeoutSeconds = 60 * sTimeoutMinutes;
        var loops = sTimeoutSeconds / (delay / 1000);
        var globalCounter = 0
        var timeOutInterval = setInterval(processSessionTimeout, delay);
        $(document).ready(function () {
            $(document).keyup(function () {
                globalCounter = 0;
                clearInterval(timeOutInterval);
                timeOutInterval = setInterval(processSessionTimeout, delay);

            });
            $('*').mouseenter(function () {
                globalCounter = 0;
                clearInterval(timeOutInterval);
                timeOutInterval = setInterval(processSessionTimeout, delay);

            });
        });
        function processSessionTimeout() {

            globalCounter += 1;
            if (globalCounter >= loops) {
                clearInterval(timeOutInterval);

                Redirect();
            }
        }


        function Redirect() {
            window.location.replace("/Pages/Account/SignIn.aspx?session=Expired");
        }
    </script>