Click here to Skip to main content
Click here to Skip to main content

The Defibrillator: Keeping ASP.NET Session Alive Ad Infinitum

, 31 May 2005
Rate this:
Please Sign up or sign in to vote.
With only two lines of code (no joking) keep your session alive as long as the user stays on your site.

Introduction to the Defibrillator

Sometimes you want to keep a session alive only as long as the user is on the site, or until they close their browser. You don't have security requirements in this situation that would make this a bad idea. You just want to keep the session timeout down, but let it persist as long as people are on your site (not just posting back to the server). I'm not kidding you when I tell you that I have figured out a way to do this with two lines of code.

Coming from a biomedical engineering background, I simply had to call this construct The Defibrillator Smile | :) You'll soon see why.

Nuts and Bolts

The most elegant engineering solutions are the simplest ones. You break down a problem into its finest components, and then focus on applying your vast knowledge to each component in order to produce a unified solution.

There are only four concepts to this solution:

  • Knowledge of the Response.
  • Knowledge of the Refresh HTTP header attribute.
  • Session property access.
  • Plain old IFrame.

You will create a dummy WebForm called Defibrillator.aspx. You do nothing with this page other than add one line on the Page_Load in the code behind file:

private void Page_Load(object sender, System.EventArgs e)
{
    Response.AddHeader("Refresh", Convert.ToString((Session.Timeout*60)-10));
}

You just added the Refresh HTTP Header Attribute to the WebForm and gave it a value of your session timeout minus ten seconds. What does that mean? It means that ten seconds before the user's session is going to timeout, as long as they have a browser open, and they are on your page, this defibrillator page will deliver massive amounts of electrical current to your server, jolting the user's session back from the brink of death...which can also be described as automatically posting back to keep the session alive Smile | :) .

Great, you say...I don't want my site to post back visibly just to keep the session alive. You don't have to. This is where the second line of code comes in.

Go into your user control that is site persistent, such as the header, or footer user control. Now add this line of code into the front end WebForm markup language:

<IFRAME id=Defib src="/Defibrillator.aspx" 
     frameBorder=no width=0 height=0 runat="server"></IFRAME>

Now this invisible Defibrillator WebForm will just sit in the IFrame and post itself back to the server 10 seconds before the session times out, only if they are still on your site or a browser is open. Check out a View Source, you won't even see the Refresh attribute because it's hanging out in the invisible IFrame.

Please use this clever little trick with discretion and an understanding of your blade resources, or else I will have an army of network administrators ready to storm my fort Smile | :)

Caveats

  • Browser Compatibility

    It will work in any browser that interprets the Refresh HTTP header. In Netscape it has the effect of hitting 'Reload' at the specified time. I have read online that this refresh will exclude your page from search engine crawling... which is fine since you put this functional tag on a dummy page in an IFrame, that you don't want anyone browsing to anyway.

  • Out of our control: precision of ASP.NET timeout on server.

    I've found that the session timeout itself in ASP.NET is not entirely accurate, especially when you are in a debug process. While I haven't had the Defibrillator fail for Session.Timeout - 10 seconds, I tested for 2 days with Session.Timeout - 5 seconds and it failed at around 36.5 hours of inactivity, because the ASP.NET session timed out before it was supposed to on the server. You may want to keep this in mind and perform your own little test. You could even fire up different browsers and let them rip, logging a time of failure and the browser type from the Request in your DB. So far so good on my app, no problems with the way I presented the solution here on Netscape or IE 6. (RE: Joe).

Extension

Write a combination of code in the defibrillator that works on a JavaScript timer, prompting the user to refresh session, and replace the Refresh HTTP header attribute with this construct. Since it is site persistent, put as much JavaScript into your .js files as possible. (RE: Fredrik)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Thomas Kurek
Web Developer
United States United States
Broad engineering background, Virginia Tech Alum. Attracted to .NET by the sheer power of the framework and elegance of c#. Work includes analytical system to calculate joint torque during balance recovery to recommend physical therapy routines and correct bad biomechanics (preventative medicine). Assimilated force plate and 3D kinematic data for modeling. First and sole developer ever to fully automate corporate housing quotes, which I built into a larger engine that serves the company's entire enterprise.

Experience with applications architecture, DB design and UML 2.0. Experience with a broad range of applied mathematics, FEM software coding, solid and fluid mechanics. Great knowledge of healthcare and physiology with degree in Mechanical/Electrical/Materials Engineering Theory (Engineering Science and Mechanics) and concentration in biomechanics.

Currently expanding my corporate housing business engine by day. By night I am expanding my software engineering design and construction knowledge, staying in touch with the direction of healthcare management systems, and moving music business technology forward. Microsoft is now working on blowing iTunes out of the water, but my business is making an even bigger revolution that is far beyond just music retail. It strikes at the heart of the music industry's issues, and perhaps we will consider partnering with Microsoft to feed their music retail goals while they feed our music licensing, publishing, and digital decentralization of distribution.

I am the Enterprise Architect at my company and maintain a small but powerful and effective team. I have personally customized CxOne from construx (www.construx.com) for this team. Long live Steve McConnell.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberchetan virkar29-May-12 3:03 
GeneralLoad issuse PinmemberPreethiSethuraman4-May-11 21:02 
GeneralSuperb Idea PinmemberVirat Kothari11-Nov-09 19:38 
Questionwill it work if browser is minimized? PinmemberPete Levine27-Aug-09 13:28 
AnswerRe: will it work if browser is minimized? PinmemberPete Levine31-Aug-09 11:39 
GeneralRe: Results I have had PinmemberDaffy91327-Aug-09 3:39 
GeneralSession in asp.net(C#) Pinmemberkva88826-Aug-09 3:03 
Generalworks when use sqlserver as backend. Pinmemberjimmimohanan8-Jun-09 0:32 
GeneralAwesome Pinmembertumitaa28-Nov-08 7:18 
Generalclass library? possible Pinmemberdsmportal14-Nov-08 4:07 
Questionhow about the db connection? Pinmemberdsmportal14-Nov-08 4:06 
Questionurgent question about this way of keeping session alive Pinmemberladypins23-Jul-08 9:05 
AnswerRe: urgent question about this way of keeping session alive PinmemberThomas Kurek23-Jul-08 10:04 
GeneralRe: urgent question about this way of keeping session alive Pinmemberladypins26-Jul-08 5:25 
GeneralRe: urgent question about this way of keeping session alive PinmemberMember 771691712-Apr-12 10:42 
Questionsilly question Pinmemberladypins9-Jul-08 7:48 
GeneralCool tips PinmemberEins Tsang8-Jul-08 23:15 
GeneralRe: Cool tips PinmemberThomas Kurek23-Jul-08 10:05 
GeneralMaybe you would also like to check out Pinmemberswedishspeeder2-Sep-07 8:06 
GeneralThanks, I really needed this solution. PinmemberBuddy Stein1-Sep-07 5:17 
GeneralRe: Thanks, I really needed this solution. PinmemberThomas Kurek1-Sep-07 6:23 
GeneralSimple and Worthy. PinmemberThiagarajan Rajendran10-Aug-07 9:59 
GeneralTime Out Issue Pinmembercoolnaveen123415-Jul-07 22:35 
AnswerRe: Time Out Issue Pinmemberpeter gabris12-Nov-07 8:32 
QuestionDoes this work with Master Pages? Pinmemberzoetosis22-May-07 23:57 
AnswerRe: Does this work with Master Pages? PinmemberThomas Kurek23-May-07 6:14 
GeneralRe: Does this work with Master Pages? Pinmemberzoetosis3-Jul-07 8:48 
QuestionWhat will happen if my page is processing some database operation? PinmemberJoshiMahesh8-Mar-07 20:44 
AnswerRe: What will happen if my page is processing some database operation? PinmemberPete O'Hanlon12-Apr-07 2:33 
QuestionHow to keep the server alive PinmemberBjørnBorg29-Sep-06 19:19 
AnswerRe: How to keep the server alive PinmemberThomas Kurek30-Sep-06 2:51 
GeneralGood Work PinmemberA.R.Ramesh12-Sep-06 13:39 
GeneralRe: Good Work PinmemberThomas Kurek17-Sep-06 15:36 
QuestionQ Pinmemberali_hamdar1-Jul-06 5:34 
AnswerRe: Q PinmemberThomas Kurek1-Jul-06 6:31 
GeneralRe: Q Pinmembermrbikejoc21-Aug-06 16:22 
GeneralVery Nice PinmemberRajan Devaraj26-May-06 9:44 
GeneralRe: Very Nice PinmemberThomas Kurek1-Jul-06 6:34 
GeneralWith FireFox not working PinmemberCalle Manthey20-Jun-05 23:53 
GeneralRe: With FireFox not working PinmemberThomas Kurek9-Jul-05 11:51 
GeneralRe: With FireFox not working PinsussAnonymous11-Jul-05 4:45 
GeneralRe: With FireFox not working PinsussAnonymous12-Aug-05 4:44 
GeneralRe: With FireFox not working Pinmemberalisona119-Sep-06 2:20 
QuestionRe: With FireFox not working Pinmemberelifk13-Nov-06 7:05 
AnswerRe: With FireFox not working Pinmembersthtnhs30-Aug-07 15:52 
AnswerRe: With FireFox not working Pinmemberkbomb98728-Jan-08 9:49 
GeneralThanx!!! PinmemberBopZ10-Jun-05 0:23 
GeneralRe: Thanx!!! PinmemberThomas Kurek10-Jun-05 2:15 
GeneralHTTP Status 204 PinmemberssaSpot9-Jun-05 5:01 
GeneralRe: HTTP Status 204 PinmemberThomas Kurek10-Jun-05 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 31 May 2005
Article Copyright 2005 by Thomas Kurek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid