Click here to Skip to main content
6,632,966 members and growing! (21,256 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Make session last forever

By Gevorg

This article will show how to make session last longer than 20 minute of inactivity.
Windows, .NET 1.0, .NET 1.1, .NET 2.0, ASP.NET, Visual Studio, Dev
Posted:28 Apr 2006
Updated:23 Oct 2006
Views:83,013
Bookmarked:64 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
45 votes for this article.
Popularity: 6.22 Rating: 3.76 out of 5
5 votes, 11.1%
1
4 votes, 8.9%
2
8 votes, 17.8%
3
4 votes, 8.9%
4
24 votes, 53.3%
5

Introduction

You are not always have a luxury to bump up the Session expiration period from 20 minutes on a server (most likely you are on shared hosting). Plus it's a bad idea to unnecessary consume the resources on a server. I have a simple solution that will take care of that problem.

Solution

The solution is simple. Lets create an aspx page that will return 1x1 transparent gif image and with javascript refresh it every 15 minutes. Thus the browser will renew the session every 15 minutes if user simply reading long text on a page or walked away to get a cup of coffee. But as soon as browser closed (or user navigated away from our site) the Session will expire right after 20 minutes of inactivity. And we do not consume the resources on a server.

Implementation

The implementation is rather simple especially in .NET environment.

1. Create renewSes.aspx page

2. In code behind have following lines

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};

        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();
        }
}
This code will simply output small 1x1 transparent gif which is in the array "gif" so we do not need to hit the file system for that. Plus the Cacheability is set to NoCache, we do not want browser to hit cache, we want it to renew our session.

3. Create user control that has following HTML

<script language="Javascript">
window.setInterval("renewSession();", 600000);
function renewSession()
{document.images("renewSession").src = "/renewSes.aspx?par=" + Math.random();}
</script>
<img src="/renewSes.aspx" name=renewSession id=renewSession>
The code simply hits /renewSes.aspx every 10 minutes. Thus keeping your session alive. Plus the Math.random was added to avoid caching in the browser.

4. Dump it on every page somewhere. Or if you are using templates then simply add it to your master page.

5. Done!!!!!!. You might want to move the JavaScript code to RegisterClientScriptBlock but it's not necessary

Conclusion

Obviously this method will only work if JavaScript is on but it's a majority, plus do not forget it's only for rare cases when user leaves your page open and walks away expecting you will keep him logged in forever. The Server resources are minimal since the sessions will continue to expire every 20 minutes when user navigated away and the only excessive traffic is those 48 bytes every ten minutes to keep the session alive.

Sample

If you want to see how it works feel free to visit my E-Commerce Body jewelry site. Add something to the shopping cart and wait for 20 minutes or for couple days. When you will be back the cart still going to be there.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gevorg


Member
I am a Senior Software developer who does a consulting work for several companies. Mostly it's an E-Commerce project.

I do have my own venture. Small online store my wife runs. Body Jewelry. Also visit Piercing info Cool thing is that nor I nor my wife has any piercings. My new website is Belly ring
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 59 (Total in Forum: 59) (Refresh)FirstPrevNext
GeneralRainbow jewels Pinmemberimaginaryplaymate23:38 20 Dec '08  
Generalthe same with asp? Pinmemberran91:26 20 Nov '07  
GeneralDreamers! Pinmemberbeep6:40 1 Nov '07  
GeneralWhy using a random number? PinmemberAdam Tibi0:13 1 Dec '06  
Generalnot fully argee PinmemberEffun Din22:36 30 Oct '06  
GeneralRe: not fully argee PinmemberAdam Tibi4:49 31 Oct '06  
GeneralRe: not fully argee PinmemberEffun Din5:17 31 Oct '06  
GeneralAnother improvement PinmemberDejaVudew4:07 10 Oct '06  
GeneralRe: Another improvement PinmemberGevorg9:41 10 Oct '06  
AnswerRe: Another improvement PinmemberDejaVudew13:01 10 Oct '06  
GeneralRe: Another improvement PinmemberGevorg9:14 12 Oct '06  
GeneralAnother aproche PinmemberZorbek14:22 9 Oct '06  
GeneralProfile? [asp.net 2.0] PinmemberBigBenDk12:48 21 Aug '06  
GeneralRe: Profile? [asp.net 2.0] PinmemberGevorg3:34 22 Aug '06  
GeneralRe: Profile? [asp.net 2.0] PinmemberMivano5:06 7 Oct '06  
GeneralSlight modification PinmemberSymo S7:11 21 Aug '06  
GeneralRe: Slight modification PinmemberTrevahaha16:25 28 Nov '07  
GeneralRe: Slight modification Pinmemberjonnoshaw17:06 3 Apr '09  
GeneralAmazing Work !! Pinmemberbobtxt23:23 8 Aug '06  
GeneralIts a good idea but here's a slight improvement PinmemberRobert Ensor18:21 30 Jul '06  
GeneralRe: Its a good idea but here's a slight improvement PinmemberGevorg5:38 31 Jul '06  
AnswerRe: Its a good idea but here's a slight improvement PinmemberAdam Tibi3:19 1 Aug '06  
GeneralRe: Its a good idea but here's a slight improvement PinmemberGevorg12:09 1 Aug '06  
GeneralRe: Its a good idea but here's a slight improvement [modified] PinmemberRobert Ensor12:53 1 Aug '06  
AnswerRe: Its a good idea but here's a slight improvement PinmemberAdam Tibi22:38 1 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Oct 2006
Editor:
Copyright 2006 by Gevorg
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project