Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating web application using .Net framework 3.5, I implemented asp.net membership (Forms Authentication), Now, What i want is when user session state is inactive then redirect page to Lockscreen.aspx and then user will enter details and continue work. After lot of search I have got something is

C#
Web config
<sessionState mode="InProc"  timeout="2"/>

<authentication mode="Forms">
      <forms defaultUrl="~/Pages/Default.aspx"
          loginUrl="~/Pages/Login.aspx" timeout="2"
          />
    </authentication>


I am configuring session timeout as when session timeout then user should get alert and if user doesn't respond then redirect to lockscreen.aspx

C#
Default.aspx

function SessionExpireAlert(timeout) {
            var seconds = timeout / 1000;
            alert(seconds);
            document.getElementsByName("secondsIdle").innerHTML = seconds;
            document.getElementsByName("seconds").innerHTML = seconds;
            setInterval(function () {
                seconds--;
                document.getElementById("seconds").innerHTML = seconds;
                document.getElementById("secondsIdle").innerHTML = seconds;
            }, 1000);
            setTimeout(function () {
                //Show Popup before 20 seconds of timeout.
                $find("mpeTimeout").show();
            }, timeout - 20 * 1000);
            setTimeout(function () {
                window.location = "LockScreen.aspx";
            }, timeout);
        }

        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        }


C#
Defualt.aspx.cs

            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);
            }


Now problem is when user session is still active session timeout alert occur's . I don't want that to be happen .Alert should appear when session is expired/timeout.
Posted
Comments
ZurdoDev 8-Jan-15 8:28am    
There is no exact method for doing this. You have the right approach, track the time in JavaScript but after every postback you need to reset the timer.
RAHUL(10217975) 8-Jan-15 8:40am    
I can't reset the timer in postback because controls I used is client side. Page load is called once when page is loaded else it's never get called. Rest everything is client side.

1 solution

you need to reset the timer on every postback, check below article : Session Time Out Warning Message Using jQuery in ASP.NET[^]
 
Share this answer
 

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