locked
Session timeout not working RRS feed

  • Question

  • User-1506965535 posted

    I have set a less than default time out for the session to check whether it is working or not. I tried with the below code:-

    <body style="padding:0px; margin:0px;" onload="initSession()";></body>
    
    <script type="text/javascript">
        var sess_pollInterval = 60000;
        var sess_expirationMinutes = "<%= Session.Timeout %>";
        var sess_warningMinutes = "<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>";
        var sess_intervalID;
        var sess_lastActivity;
    
        function initSession() {   
       
            sess_lastActivity = new Date();
            sessSetInterval();
    //        $(document).bind('keypress.session', function (ed, e) {
    //            sessKeyPressed(ed, e);
    //        });
        }
    
        function sessSetInterval() {
            sess_intervalID = setInterval('sessInterval()', sess_pollInterval);
        }
    
        function sessClearInterval() {
            clearInterval(sess_intervalID);
        }
    
        function sessKeyPressed(ed, e) {
            sess_lastActivity = new Date();
        }
    
        function sessLogOut() {
        window.open('../../FrmCrm_Logout.aspx',"_self");
        }
    
        function sessInterval() {
            var now = new Date();
            //get milliseconds of differneces 
            var diff = now - sess_lastActivity;
            //get minutes between differences
            var diffMins = (diff / 1000 / 60);
    
            if (diffMins >= sess_warningMinutes) {
                //wran before expiring
                //stop the timer
                sessClearInterval();
                //promt for attention
                var active = confirm('Your session will expire in ' + 
                    (sess_expirationMinutes - sess_warningMinutes) +
                    ' minutes (as of ' + now.toTimeString() + '), \npress OK to remain logged in ' + 
                    'or press Cancel to log off. \nIf you are logged off any changes will be lost.');
                if (active == true) {
                    now = new Date();
                    diff = now - sess_lastActivity;
                    diffMins = (diff / 1000 / 60);
    
                    if (diffMins > sess_expirationMinutes) {
                        sessLogOut();
                    }
                    else {
                    window.open("../../FrmCrm_SessionIncrease.aspx", 'oNewWin', 'left=10,top=10,height=100,width=200px,resizable=no,scrollable=no,scrollbars=no');
                        initSession();
                        sessSetInterval();                   
                        sess_lastActivity = new Date();
                    }
                }
                else {
                    sessLogOut();
                }
            }
        }
            
    </script>

    and in web.config i have set like below

    <sessionState mode="InProc" cookieless="false" timeout="30" />

    but It is not working for me.

    Monday, January 18, 2016 9:17 AM

Answers

  • User-986267747 posted

    Hi Nadeem157,

    but It is not working for me.

    I'm not sure what you mean. Could you describe your meaning? According to your code, i create a sample, i find it works fine, we could get the expiration Minutes and when the session timeout, we could open a new page. You could use the F12 Developer Tools to debug your code in client side and check if you could fire the related js function or you get some error message.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionTime.aspx.cs" Inherits="GridViewAppear.SessionTime" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
          <script type="text/javascript">
              var sess_lastActivity = new Date();
              var exp = "<%= Session.Timeout %>";
            function sessSetInterval() {
                sess_intervalID = setInterval('test()', 1000);
            }
    
            function test() {
                var now = new Date();
                var diff = now - sess_lastActivity;
                //get minutes between differences
                var diffMins = (diff / 1000 / 60);
    
                if (diffMins > exp) {
                    alert("time out");
                }
    
            }
        </script>
    </head>
    <body onload="sessSetInterval()">
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2016 5:13 AM

All replies

  • User-986267747 posted

    Hi Nadeem157,

    but It is not working for me.

    I'm not sure what you mean. Could you describe your meaning? According to your code, i create a sample, i find it works fine, we could get the expiration Minutes and when the session timeout, we could open a new page. You could use the F12 Developer Tools to debug your code in client side and check if you could fire the related js function or you get some error message.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionTime.aspx.cs" Inherits="GridViewAppear.SessionTime" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
          <script type="text/javascript">
              var sess_lastActivity = new Date();
              var exp = "<%= Session.Timeout %>";
            function sessSetInterval() {
                sess_intervalID = setInterval('test()', 1000);
            }
    
            function test() {
                var now = new Date();
                var diff = now - sess_lastActivity;
                //get minutes between differences
                var diffMins = (diff / 1000 / 60);
    
                if (diffMins > exp) {
                    alert("time out");
                }
    
            }
        </script>
    </head>
    <body onload="sessSetInterval()">
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2016 5:13 AM
  • User-1506965535 posted

    Hi Klein,

    Actually it was working, when I tested it on working server by creating a test page.

    The difference in my local page was that, I didn't entered the userid for starting the session

    Tuesday, January 19, 2016 5:45 AM