Click here to Skip to main content
15,849,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The system should monitor user activity and it should start the countdown only if it sees that the user is not doing anything say for 3 mins or 5 mins
And when the countdown is running lets say for 1 hour and user clicks inside the page and does some action, then the countdown should reset to 2 hour and it should not start the countdown until it again detects user inaction for 3 or 5 mins
If there is user inaction for 3 or 5 mins and the countdown starts and runs till 1:58 mins and still there is no user action, then the popup with 2 min countdown should appear with "Extend Session" button
With “Extend Session” button click the counter should be reset to 2 hours and again it should not start the countdown until it detects user inaction for 3 or 5 mins

What I have tried:

<body>
    <form id="form1" runat="server">
    <h3>
        Session Idle: <span id="secondsIdle"></span> seconds.</h3>
    <div id="dialog">
        Your Session will expire in <span id="seconds"></span> seconds.<br />
        Do you want to reset?
    </div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
        rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function () {
            $("#dialog").dialog({
                autoOpen: false,
                modal: true,
                title: "Session Expiring",
                buttons: {
                    Ok: function () {
                        ResetSession();
                    },
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
        });
        function SessionExpireAlert(timeout) {
            var seconds = timeout / 1000;
            $("#secondsIdle").html(seconds);
            $("#seconds").html(seconds);
            setInterval(function () {
                seconds--;
                $("#secondsIdle").html(seconds);
                $("#seconds").html(seconds);
            }, 1000);
            setTimeout(function () {
                //Show Popup before 20 seconds of timeout.
                $('#dialog').dialog('open');
            }, timeout - 30 * 1000);
            setTimeout(function () {
                window.location = "User/SessionExpired";
            }, timeout);
        };
        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        };
    </script>
    </form>
</body>


-------------------------------------------------------------------------------------
cs page

Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (!this.IsPostBack)
{
Session["Reset"] = true;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
Posted
Updated 5-May-21 0:54am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900