Scott on Writing

Musings on technical writing...

Watch Your Cookies!

RCF 2109 - HTTP State Management Mechanism - provides the standard for how browsers should handle cookies.  Near the end of the RFC, the following recommendation is made:

Practical user agent implementations have limits on the number and size of cookies that they can store.  In general, user agents' cookie support should have no fixed limits.  They should strive to store as many frequently-used cookies as possible.  Furthermore, general-use user agents should provide each of the following minimum capabilities individually, although not necessarily simultaneously:

  • at least 300 cookies
  • at least 4096 bytes per cookie (as measured by the size of the characters that comprise the cookie non-terminal in the syntax description of the Set-Cookie header)
  • at least 20 cookies per unique host or domain name

On KB article 306070, Number and Size Limits of a Cookie in Internet Explorer, Microsoft states that they “comply” with the RFC by providing the bare minimum cookie requirements.  Urm, hello, Microsoft... these are the minimum capabilities.  Perhaps the goal here was not to have to have different cookie-handling code for IE based on the device (for example, a hand held device or cell phone might need to place such size/length restrictions on cookies), but it seems a bit annoying that such limitations should be placed on IE 6 when being used on my desktop, which has dozens of gigabytes just waiting to hold cookie data.  (There is a workaround for the 20 cookie limit - namely, use a cookie dictionary.  See the KB article for more info.)

Anyway, this 20 cookie limit came to bite me in the behind.  The scenario: a website using forms-based authentication and a Web page using Telerik's r.a.d. spell control.  Each r.a.d. spell control on a site emits a session-based cookie for the purpose of 'securing the dialog.'  I'm not 100% clear what that means or what, exactly, is being secured.  Initially I assumed that it had something to do with the client-side spell checking dialog, ensuring that some cross-site script or some other attack couldn't be used to read or modify the contents while being spelled checked.  That was my guess, at least, until I found that there's a UseSession property that will store whatever is needed to ensure the 'security' in a session variable.  Hrm, so it's a server-side check... I'm confused, but I digress.

Anywho, the UseSession property is False by default, so by default each time a user visits a page that has a spell checker control, or visits a page with many spell checker controls, new cookies get added to the browser.  You can probably see where I'm heading with this - one particular page had over 40 spell checking control instances on it which proceeded to write 40+ cookies to the client.  Since IE can only remember 20, it conveniently decided to nix my forms-based authentication ticket cookie.  The end result?  Whenever I visited this page, after posting back I'd immediately be kicked back to the logon page because the authentication ticket was getting overwritten by the slew of r.a.d. spell cookies.  Meh.

The workaround, in my case, was to simply set the UseSession property to True, thereby avoiding the plethora of r.a.d. spell cookies and thereby preserving my forms-based authentication ticket.

It took a bit of researching to figure this particular one out, and it wouldn't have been possible without Fiddler, a great tool for inspecting the incoming & outgoing HTTP streams.  (I mentioned Fiddler a few weeks back in my Scott Hanselman's Recommended Tools blog entry.) Thanks to Fiddler I was able to see the mess of cookies being added by r.a.d. spell and the corresponding loss of my forms-based authentication ticket cookie.  In addition, I set a breakpoint on the Global.asax's AuthenticateRequest event handler and added to the Watch window Request.IsAuthenticated.  On postback, then, I would hit the breakpoint and see that I had 'lost' my authenticated status.

In any event, if you find yourself being 'automagically' logged out of your site, perhaps it may be due to using IE as your browser and having too many cookies from your domain.  Hope this helps someone save the time that I invested.

Happy Programming!

posted on Monday, July 18, 2005 5:14 PM

Feedback

# re: Watch Your Cookies! 7/18/2005 7:08 PM Keith J. Farmer

While they're at it, think they'll get proxies to stop truncating long form variables, such as viewstate?

# re: Watch Your Cookies! 7/19/2005 2:46 AM Morgan Roderick

Scott,

I've seen this happen to people a few times before. Having web controls emit their own cookies is, in my opinion, bad form. One cookie per page should be sufficient, since youre not supposed to store data in cookies anyway.

Keith,

Or perhaps you should ask yourself whether you really need the massive hack that the viewstate is, or if there is a simpler way of achieving the image of stateful controls.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles