Web.config session timeout

S

steph_mw

I have a web application and when a user opens the application (using
windows integration) and loads some data, it loads a ID number into the
session state. If for some reason they go away from their PC and come
back after 20 mintes they are receiving an error.

This error relates to the fact that the session has timed out and all
session variables have been cleared. The only issue I have with this is
that the session timeout is set to 24 hours (1440 minutes). I
understand that this is the maximum value.

<sessionState mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="1440" />

We have no issue with the session variables been open this long as the
server has available memory.

Can anyone help me with this issue or point me in the right direction.

TIA
Stephen
 
G

George Ter-Saakov

Not sure why the session times out even if you set it to 1440 but i have a
good solution for you.

The idea is to send an aspx request to renew session every 10 minutes with
javascript code. So if user has browser open the session will never time
out. If browser closed session will timeout after 20 seconds.

See code below.

Step 1.
The code for renewSes.aspx.cs. You must not have any HTML in renew.aspx
file.

public class renewSes : System.Web.UI.Page

{

static byte [] gif =
{0x47,0x49,0x46,0x38,0x39,0x61,0x01,0x00,0x01,0x00,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xf9,0x04,0x09,0x00,0x00,0x00,0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x08,0x04,0x00,0x01,0x04,0x04,0x00,0x3b,0x00};

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}




private void InitializeComponent()

{

Response.AddHeader("ContentType", "image/gif");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.BinaryWrite(gif);

Response.End();

}

#endregion

}





Step2.

Each page must have following script in the beginning

<script language="Javascript">

window.setInterval("renewSession()", 600000);

function renewSession()

{

document.images("renewSession").src = /renewSes.aspx?par=" + Math.random();

}

</script>

Step3.

Each page must have somewhere in HTML

<IMG name="renewSession" height=1 src="/images/transparentpixal.gif" width=1
border=0>

You can grab transparentpixal.gif from
http://www.cardone.com/English/Club/images/transparentpixal.gif

-------------------------------------------------------------------------------------------------------------

For step2 and step 3 I usually create UserControl that does in a code in
Init method

Page.RegisterClientScriptBlock("renewSession", "<script
language=\"Javascript\">\r\n" +

"window.setInterval(\"renewSession();\", 600000);\r\n" +

"function renewSession()\r\n" +

"{document.images(\"renewSession\").src = \"/renewSes.aspx?par=\" +
Math.random();}\r\n" +

"</script>\r\n");

And HTML has that line with <IMG tag>

Then all you need to do is to drop that control on each page in your
project.

George.
 
S

steph_mw

Thanks George for your solution.

However I would like to know why it still times out when the timeout
value is stored in the web.config file. If anyone can shed some light
on this it'd be appreciated.

Thanks
Stephen
 
G

George Ter-Saakov

Do not really know answer to your question.
May be you set to high. May be it can not be more than couple hours and
resets itself to default if you try to set it to high.

Try to set it to 60 and see if it's still going to timeout after 20 minutes.

-------------------------------------------------------------------------

but theoretically you should never change it. Just forget that it's even
possible :)


George.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top