locked
Prevent session timeout RRS feed

  • Question

  • User-2038671627 posted

    I am developing an application using vb.net. As I develop, I refresh the live view to see changes. After about 10 refreshes, the session expires and I get errors because session variables loose their values and queries fail to run correctly because some value no longer exists. This causes problems as I then have to go back to the start of the application and run through a sequence of pages to get back to where I was.

    My app is a an event registration site that allows registrants to pay event fees via PayPal. I'm working on the receipt and every time the session expires, I have to make another purchase, and payment to get to the receipt. Luckily PayPal allows me to not get charged by using their sandbox testing site.

    I have already included the code below on all pages in the application to prevent the session from expiring. What else can I do? 

    <% Session.Timeout = 480%>

    Michael

    Friday, November 20, 2015 10:32 AM

Answers

  • User614698185 posted

    Hi Michael,

    If you are trying to stop the session from timing out all the time you can do this rather than increasing the session timeout.

    KeepAlive.aspx:

    <%@ OutputCache Location="None" VaryByParam="None" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        KEEP ALIVE
        </div>
        </form>
    </body>
    </html>

    Keep Alive.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //Add refresh header to refresh the page 60 seconds before session timeout
            Response.AddHeader("Refresh", Convert.ToString((Session.Timeout * 60) - 60));
        }
        catch (Exception)
        {
             throw;
        }
    }

    Then in your master page create an iFrame that refreshes to keep the session alive:

    <iframe id="Defib" src="KeepAlive.aspx" frameborder="0" width="0" height="0" runat="server">
    </iframe>

    Please see: http://weblogs.asp.net/stevewellens/ah-ah-ah-ah-staying-alive-staying-alive

    http://www.codeproject.com/Articles/9607/Prevent-Session-Timeout-in-ASP-NET

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 23, 2015 3:20 AM
  • User-1716253493 posted

    Add this in the head tag

    <meta http-equiv="refresh" content="30">

    this will refresh your page every 30 seconds

    refresh the page before session get time out i.e 5 minutes

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 25, 2015 7:46 AM

All replies

  • User475983607 posted

    Session is volatile by it's very definition.  

    A general Session usage pattern is to detect if Session is null before using Session.  If Session is null then repopulate Session.  If the application is unable to repopulate Session easily then you should reconsider the design or persist Session in SQL.

    https://msdn.microsoft.com/en-us/library/ms178581.aspx

    https://support.microsoft.com/en-us/kb/317604

    Friday, November 20, 2015 10:48 AM
  • User-2038671627 posted

    With a little research, I found out that the application is recompiling because files are changing as I develop. After some recompiling, the session is lost.

    What I will do is add a few lines of code to the receipt to set the session variables when the page loads. This will rebuild the session every time. when I complete development, I will remove these lines of code and let the app function as expected.

    Friday, November 20, 2015 10:57 AM
  • User475983607 posted

    With a little research, I found out that the application is recompiling because files are changing as I develop. After some recompiling, the session is lost.

    What I will do is add a few lines of code to the receipt to set the session variables when the page loads. This will rebuild the session every time. when I complete development, I will remove these lines of code and let the app function as expected.

    You were warned..

    Friday, November 20, 2015 11:01 AM
  • User-2038671627 posted

    I see what you are saying, but this is only an issue when I am developing, not in normal function.

    Friday, November 20, 2015 11:25 AM
  • User614698185 posted

    Hi Michael,

    If you are trying to stop the session from timing out all the time you can do this rather than increasing the session timeout.

    KeepAlive.aspx:

    <%@ OutputCache Location="None" VaryByParam="None" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        KEEP ALIVE
        </div>
        </form>
    </body>
    </html>

    Keep Alive.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //Add refresh header to refresh the page 60 seconds before session timeout
            Response.AddHeader("Refresh", Convert.ToString((Session.Timeout * 60) - 60));
        }
        catch (Exception)
        {
             throw;
        }
    }

    Then in your master page create an iFrame that refreshes to keep the session alive:

    <iframe id="Defib" src="KeepAlive.aspx" frameborder="0" width="0" height="0" runat="server">
    </iframe>

    Please see: http://weblogs.asp.net/stevewellens/ah-ah-ah-ah-staying-alive-staying-alive

    http://www.codeproject.com/Articles/9607/Prevent-Session-Timeout-in-ASP-NET

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 23, 2015 3:20 AM
  • User-1716253493 posted

    Add this in the head tag

    <meta http-equiv="refresh" content="30">

    this will refresh your page every 30 seconds

    refresh the page before session get time out i.e 5 minutes

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 25, 2015 7:46 AM