Y'herd thisun? 

“The main requirement is for commerce to replace politics as the driving force behind space policy”

from Spaceflight Revolution by David Ashford

Kill the web backcache

TaggedDotNet, ASP.Net

Still caching after all these years

I was making a forms based ASP.Net login system, a pretty typical one that used an ascx across all pages to display a hyperlink "Log In" or "Log Out" depending on whether HttpContext.Current.User.Identity.Name had a value or not. The "Log In" link would redirect to a logon page and the Log Off link would of course redirect to a page that killed the authentication cookie in it's Page_Load and gave the user the message that they were logged off successfuly.

When I did the log offs, I killed the ticket and just for good measure called Session.Abandon, and with that extra line I felt smug that I did more than I needed.

Since this is such a common bit of code I guess I just didn't look too close at the hyperlinks during my unit testing. But my tester did (god love him ... to death).

Seems if the user hit the Back button after logging off, they'd get back to the page and still see the "Log Off" link implying that they were still logged on. The cookie was gone and they were logged off, but it was confusing.

Nope, had nothing to do with Postback since the hyperlink controls weren't in a form.

It was my habitual use of HTML META tags to force the browser to not cache.

<META HTTP-EQUIV="Expires" CONTENT="Sat, 01 Jan 2000 12:01:01 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" Content="no-cache">

I don't know where I picked those three lines of code up back in the ASPClassic days but they've stuck with me. And I think that they've been working all along. But not this time. Maybe it's because the rule of thumb that newer browsers don't cache ASPXes by default is only a myth, or maybe it's because I was always behind locked down proxy servers in the past (unlikely, but even still the Pragma is supposed to take care of that). Whatever the case, those lines from pre ASP didn't work, go figure.

So now, I do it this way... and even though it appears to be exactly the same, doing it in "real code" seems to work better on IE6 Back button events:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private sub SetNoCaching()
Response.AddHeader("Pragma", "no-cache") 
Response.Cache.SetExpires( DateTime.Now.AddSeconds(-1) )
End Sub

Robert Smith
Kirkland, WA

home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better