Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to show session expiry alert before 60 seconds. It's working fine.

if we have set session time out 2 minutes and alert will come before 60 seconds. But user are fill a form and it will take 5 to 10 minutes means user are active on my application then in this case how to reset session .
in my current code session will be reset only on server side hit not client side activites.
Please help.

What I have tried:

Inside web config-- 
 <sessionState timeout="5"/>

 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true" ScriptMode="Release" AsyncPostBackTimeout="150">
                    </asp:ScriptManager>
                        <asp:LinkButton ID="lnkFake" runat="server" />
                        <asp:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout" runat="server" PopupControlID="pnlPopup"
                            TargetControlID="lnkFake" OkControlID="btnYes"  BackgroundCssClass="modalBackground"
                            OnOkScript="ResetSession()">
                        </asp:ModalPopupExtender>
                        <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
                            <div class="header">
                                Session Expiring!
                            </div>
                            <div class="body">
                                Your Session will expire in <span id="seconds"></span> seconds.<br />
                                Do you want to reset?
                            </div>
                            <div class="btnSection" align="right">
                                <asp:Button ID="btnYes" runat="server" Text="OK" CssClass="yes" />
                            </div>
                        </asp:Panel>
                        <script type="text/javascript">
                            var myTimer;
                            var myTimer2;
                            var myTimer3;
                            function SessionExpireAlert(timeout) {
                                var seconds = timeout / 1000;            
                                document.getElementsByName("seconds").innerHTML = seconds;
                                myTimer = setInterval(function () {
                                    seconds--;
                                    document.getElementById("seconds").innerHTML = seconds;                            
                                }, 1000);
                                myTimer2=setTimeout(function () {
                                    //Show Popup before 20 seconds of timeout.
                                    $find("mpeTimeout").show();
                                }, timeout - 60 * 1000);
                                myTimer3 = setTimeout(function () {                                 
                                    window.location = '<%=ResolveUrl("~/CRM/Login.aspx") %>';
                                }, timeout);
                            };
                            function ResetSession() {
							// hit web method to retain data in form
                                $.ajax({
                                    type: "POST",
                                    contentType: "application/json; charset=utf-8",
                                    url: '<%=ResolveUrl("~/CRM/Login.aspx/ResetSession") %>',
                                    dataType: "json",
                                    success: function (data) {
                                        clearInterval(myTimer);
                                        clearInterval(myTimer2);
                                        clearInterval(myTimer3);
                                        SessionExpireAlert(data.d);
                                    },
                                    error: function (result) {
                                        alert("Error");
                                    }
                                });
                            }
                        </script>
						
============Login.aspx.cs----
  protected void Page_Load(object sender, EventArgs e)
    {

           Response.Cache.SetCacheability(HttpCacheability.NoCache);
                if (!Page.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;
                    ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
                }
	}			
[WebMethod]
    public static string ResetSession()
    {
        int TimeOut = 0;
        try
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);// To handle Session Timeout Functionality
            HttpContext.Current.Session["Reset"] = true;
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
            SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
            TimeOut = (int)section.Timeout.TotalMinutes * 1000 * 60;
            //ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
        }
        catch (Exception ex)
        {

        }
        return TimeOut.ToString();
    }						
Posted
Updated 23-Mar-17 6:47am
Comments
F-ES Sitecore 23-Mar-17 5:59am    
First off trying to do this is pointless. Your client has no idea when the session is going to end so what you're doing is impossible. Second off I'd ask what it is you are ultimately trying to achieve, ie what benefit are you trying to add? And lately what is your actual question?
suneel kumar gupta 23-Mar-17 6:10am    
If i have set session time out 10 minutes and till the 9 minutes system is in Idle status then a alert will come to reset session otherwise you need to login again in system. Once you have reset session then alert will come after next 10 minutes.
F-ES Sitecore 23-Mar-17 6:50am    
What if I have multiple tabs? What if I have gone "back" from a previous page? What if IIS closes the session prematurely as it needs the resources? These things could all throw off your timer. If something isn't going to work in all scenarios then there isn't much point in doing it.

As for your question it's less likely someone is going to go through your code dump to set it up and debug your code for you. Use the debugger and the browser tools to at least try and figure out the flow of your code and where it is going wrong.

1 solution

There is no way to accurate do it; however, what you can do is create a WebMethod that has Session enabled and anytime client-side activities are done you can call this WebMethod using jQuery's .ajax() call which will reset the session server side without having to post back.

And then reset your timer in JavaScript.
 
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