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   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles