HomeAsp.Net

Prevent Session Timeout in Asp.net

Like Tweet Pin it Share Share Email

Prevent Session Timeout in Asp.net [VB]

ASP.Net 2.0 [VB]

Inspired by this article from Code Project. [Thanks Ach1lles ]


I slightly modified few things to work for VB and VS2005 environment. The code works perfect and I’m very happy with it.First I created a Module file and created this function.
[sourcecode language=”vb”]

Public Function KeepAlive() As String

        Dim int_MilliSecondsTimeOut As Integer = (HttpContext.Current.Session.Timeout * 60000) – 30000
        Dim sScript As New StringBuilder
        sScript.Append(“")

        KeepAlive = sScript.ToString
End Function

[/sourcecode]
All this code does is, build a simple javascript function and its called from server side.The works like this,- Gets the current session timeout duration.
- Subtract 30 seconds from it and assign it to MilliSecondsTimeOut variable
- Create a Reconnect() javascript function
- Create a global variable with max value 6
- Get current hour and Min [just for displaying the last refresh time]
- Verify whether count is less than max value (ie 6)
- If so, change the window status with Text
- Create a dummy image and set reconnect.aspx url as its source [this way a call is made to server and it wont session timeout]Create a timer using window.setInterval and assign the MilliSecondsTimeOut value.
[this way this function is called 30 seconds before session timeout]Tha max value (6) can be set to any number. If its 6 then this session timeout is avoided 5 times.

Default session timeout is 20min. 20 * 5 = 100 mins. Which is good for non-secure page.

Then

On the pages where I want to prevent Session Timeout I entered this line.

[sourcecode language="vb"]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
'dont forget to add this line 
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "reconn key", KeepAlive())
 
 If Not Page.IsPostBack Then
 ---
 ---
 ---
 End If

Catch eX as Exception
 '----
End Try
End Sub

[/sourcecode]
Created reconnect.aspx in Visual Studio and deleted the .vb and .designer.vb files.This is the final version of reconnect.aspx
[sourcecode language="html"]

< %@ Page Language="vb" AutoEventWireup="false"%>
< %@ OutputCache Location="None" VaryByParam="None" %>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml" >

[/sourcecode]
Note : When you compile the project sometimes, VS2005 complains reconnect.aspx is not in right format.
So I EXCLUDED this file from my project. Now VS 2005 will compile without complaining and the logic will also work.- Happy Programming

Comments (5)

  • Interesting method of detecting session timeout, one that I’ve never seen before.

    Reply
  • I was wondering if you could write a program for me for time out not to happen.

    Could you help me with that. I would pay you and it’s nothing bad (or for wrong purposes).

    thanks,

    David

    Reply
  • Hi David,

    I don’t think its advisable to make time out not to happen. The server will have overload with endless sessions.

    You can increase the session timeout in IIS server, if IIS is not in your control, then you can use code like this and increase the timeout duration.

    ‘sScript.Append(“var max = 6;” & vbNewLine)

    The default timeout is 20 minutes. This code prevents it 5 times which means 5 * 20 = 100 mins.

    You can change the number to more than 6. (Its not advisable to keep it active for long duration for the reason said above)

    Hope this answer helps you.

    Reply
  • Hi,

    I am trying to take a slightly different approach. In my case, I am setting the timeout value to 60 minutes in my web.config file. However, the application is still timing out after 20 minutes. What am I missing?

    Thanks,

    Scott

    Reply
  • Hey guys,

    In my office we are using citrix remote desktop to work on the client’s server. My problem is if the system is inactive for sometime “Protocol drive error” is occuring and we have to re-login to citrix. And there is loss of data when there is unsaved documents.

    This is really irritating. Anyone having solution to this problem. Please help me in this 🙂

    Jeni

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *