Click here to Skip to main content
11,920,394 members (60,333 online)
Click here to Skip to main content
Add your own
alternative version


104 bookmarked

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 | :)


  • 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).


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)


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


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 ( for this team. Long live Steve McConnell.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
chetan virkar29-May-12 3:03
memberchetan virkar29-May-12 3:03 
GeneralLoad issuse Pin
PreethiSethuraman4-May-11 21:02
memberPreethiSethuraman4-May-11 21:02 
GeneralSuperb Idea Pin
Virat Kothari11-Nov-09 19:38
memberVirat Kothari11-Nov-09 19:38 
Questionwill it work if browser is minimized? Pin
Pete Levine27-Aug-09 13:28
memberPete Levine27-Aug-09 13:28 
AnswerRe: will it work if browser is minimized? Pin
Pete Levine31-Aug-09 11:39
memberPete Levine31-Aug-09 11:39 
GeneralRe: Results I have had Pin
Daffy91327-Aug-09 3:39
memberDaffy91327-Aug-09 3:39 
GeneralSession in Pin
kva88826-Aug-09 3:03
memberkva88826-Aug-09 3:03 
Generalworks when use sqlserver as backend. Pin
jimmimohanan8-Jun-09 0:32
memberjimmimohanan8-Jun-09 0:32 
GeneralAwesome Pin
tumitaa28-Nov-08 7:18
membertumitaa28-Nov-08 7:18 
Generalclass library? possible Pin
dsmportal14-Nov-08 4:07
memberdsmportal14-Nov-08 4:07 
Questionhow about the db connection? Pin
dsmportal14-Nov-08 4:06
memberdsmportal14-Nov-08 4:06 
Questionurgent question about this way of keeping session alive Pin
ladypins23-Jul-08 9:05
memberladypins23-Jul-08 9:05 
AnswerRe: urgent question about this way of keeping session alive Pin
Thomas Kurek23-Jul-08 10:04
memberThomas Kurek23-Jul-08 10:04 
GeneralRe: urgent question about this way of keeping session alive Pin
ladypins26-Jul-08 5:25
memberladypins26-Jul-08 5:25 
GeneralRe: urgent question about this way of keeping session alive Pin
Member 771691712-Apr-12 10:42
memberMember 771691712-Apr-12 10:42 
Questionsilly question Pin
ladypins9-Jul-08 7:48
memberladypins9-Jul-08 7:48 
GeneralCool tips Pin
Eins Tsang8-Jul-08 23:15
memberEins Tsang8-Jul-08 23:15 
GeneralRe: Cool tips Pin
Thomas Kurek23-Jul-08 10:05
memberThomas Kurek23-Jul-08 10:05 
GeneralMaybe you would also like to check out Pin
swedishspeeder2-Sep-07 8:06
memberswedishspeeder2-Sep-07 8:06 
GeneralThanks, I really needed this solution. Pin
Buddy Stein1-Sep-07 5:17
memberBuddy Stein1-Sep-07 5:17 
GeneralRe: Thanks, I really needed this solution. Pin
Thomas Kurek1-Sep-07 6:23
memberThomas Kurek1-Sep-07 6:23 
GeneralSimple and Worthy. Pin
Thiagarajan Rajendran10-Aug-07 9:59
memberThiagarajan Rajendran10-Aug-07 9:59 
GeneralTime Out Issue Pin
coolnaveen123415-Jul-07 22:35
membercoolnaveen123415-Jul-07 22:35 
AnswerRe: Time Out Issue Pin
peter gabris12-Nov-07 8:32
memberpeter gabris12-Nov-07 8:32 
QuestionDoes this work with Master Pages? Pin
zoetosis22-May-07 23:57
memberzoetosis22-May-07 23:57 
AnswerRe: Does this work with Master Pages? Pin
Thomas Kurek23-May-07 6:14
memberThomas Kurek23-May-07 6:14 
GeneralRe: Does this work with Master Pages? Pin
zoetosis3-Jul-07 8:48
memberzoetosis3-Jul-07 8:48 
QuestionWhat will happen if my page is processing some database operation? Pin
JoshiMahesh8-Mar-07 20:44
memberJoshiMahesh8-Mar-07 20:44 
AnswerRe: What will happen if my page is processing some database operation? Pin
Pete O'Hanlon12-Apr-07 2:33
memberPete O'Hanlon12-Apr-07 2:33 
QuestionHow to keep the server alive Pin
BjørnBorg29-Sep-06 19:19
memberBjørnBorg29-Sep-06 19:19 
AnswerRe: How to keep the server alive Pin
Thomas Kurek30-Sep-06 2:51
memberThomas Kurek30-Sep-06 2:51 
GeneralGood Work Pin
A.R.Ramesh12-Sep-06 13:39
memberA.R.Ramesh12-Sep-06 13:39 
GeneralRe: Good Work Pin
Thomas Kurek17-Sep-06 15:36
memberThomas Kurek17-Sep-06 15:36 
QuestionQ Pin
ali_hamdar1-Jul-06 5:34
memberali_hamdar1-Jul-06 5:34 
AnswerRe: Q Pin
Thomas Kurek1-Jul-06 6:31
memberThomas Kurek1-Jul-06 6:31 
GeneralRe: Q Pin
mrbikejoc21-Aug-06 16:22
membermrbikejoc21-Aug-06 16:22 
GeneralVery Nice Pin
Rajan Devaraj26-May-06 9:44
memberRajan Devaraj26-May-06 9:44 
GeneralRe: Very Nice Pin
Thomas Kurek1-Jul-06 6:34
memberThomas Kurek1-Jul-06 6:34 
GeneralWith FireFox not working Pin
Calle Manthey20-Jun-05 23:53
memberCalle Manthey20-Jun-05 23:53 
GeneralRe: With FireFox not working Pin
Thomas Kurek9-Jul-05 11:51
memberThomas Kurek9-Jul-05 11:51 
GeneralRe: With FireFox not working Pin
Anonymous11-Jul-05 4:45
sussAnonymous11-Jul-05 4:45 
GeneralRe: With FireFox not working Pin
Anonymous12-Aug-05 4:44
sussAnonymous12-Aug-05 4:44 
GeneralRe: With FireFox not working Pin
alisona119-Sep-06 2:20
memberalisona119-Sep-06 2:20 
QuestionRe: With FireFox not working Pin
elifk13-Nov-06 7:05
memberelifk13-Nov-06 7:05 
AnswerRe: With FireFox not working Pin
sthtnhs30-Aug-07 15:52
membersthtnhs30-Aug-07 15:52 
AnswerRe: With FireFox not working Pin
kbomb98728-Jan-08 9:49
memberkbomb98728-Jan-08 9:49 
GeneralThanx!!! Pin
BopZ10-Jun-05 0:23
memberBopZ10-Jun-05 0:23 
GeneralRe: Thanx!!! Pin
Thomas Kurek10-Jun-05 2:15
memberThomas Kurek10-Jun-05 2:15 
GeneralHTTP Status 204 Pin
ssaSpot9-Jun-05 5:01
memberssaSpot9-Jun-05 5:01 
GeneralRe: HTTP Status 204 Pin
Thomas Kurek10-Jun-05 2:07
memberThomas Kurek10-Jun-05 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web03 | 2.8.151120.1 | Last Updated 31 May 2005
Article Copyright 2005 by Thomas Kurek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid