find a location for property in a new city

Monday 21 February 2011

Multiple duplicate cookies and one blank cookie

I noticed we had a problem where there were duplicate cookies on my site. One was correct but the other was carrying an empty string.

It took a lot of debugging and use of Fiddler but I found out something very odd with the .NET framework that was causing this problem.

The oddity

It would seem that evaluating the response's cookies creates one if it doesn't already exist. Here's an example:
if (Response.Cookies["myCookie"] == null)
{
    var cookie = new HttpCookie("myCookie", "myValue");
    cookie.Domain = ".britishdeveloper.co.uk";
    cookie.Expires = DateTime.Now.AddDays(7);
    Response.Cookies.Add(cookie);
}
//relax because you now have a cookie with the intended domain and expiry. Right?

Line 1 actually creates the myCookie cookie! If you ever evaluate the property indexer it will create that cookie with no value, no expiration, no domain. Nothing, but it will still exist! Fiddler will show you this in your Response headers: Set-Cookie: myCookie. Also, line 1 will always evaluate to false because of this.

How to avoid the blank cookie

If you are checking for its existence you should NOT use the index property as it will make a new cookie if it doesn't exist.

Instead, use this code that star developer Leo discovered to resolve the issue:
if (!Response.Cookies.AllKeys.Contains("myCookie"))
{
    var cookie = new HttpCookie("myCookie", "myValue");
    cookie.Domain = ".britishdeveloper.co.uk";
    cookie.Expires = DateTime.Now.AddDays(7);
    Response.Cookies.Add(cookie);
}
//relax because you now have a cookie with the intended domain and expiry. Right? YES!

Line 1 now evaluates if myCookie exists in the collection of all response cookies rather than accessing (and creating) it directly. So now you know, hopefully it won’t cause you the hours of debugging it cost myself and Leo.

Note: This is not true of Request.Cookies - only Response.Cookies. Lovely bit of inconsistency there

Follow britishdev on Twitter

6 comments:

  1. Thank you! I was having the same problem...

    ReplyDelete
  2. Thanks for the great tip!

    ReplyDelete

  3. Thanks for posting this useful content, Good to know about new things here, Let me share this, . Hadoop training in pune

    ReplyDelete
  4. Nice Blog.Thank you for Sharing. We are leading erp software software solution providers in chennai. For more details call +91 9677025199. erp software in chennai | erp providers in chennai | online events registration

    ReplyDelete

  5. Buy Real Fake Documents is your fake certificates online destination for the most requested documents. Whether you are here to replace a damaged, stolen or fool somebody with a realistic novelty, we have fake certificates options to assist your needs. You can find everything from buy real fake documents like buy real and fake passport online, buy fake driving license online, fake birth certificates, make ID cards online, buy death certificates online. We even carry educational documents including popular favourites such as IELTS, International student identity card (ISIC), British national insurance number, city, guilds and more.

    For more details you can visit our website: https://buyalldocument.com/ OR WhatsApp: +1(443) 351-8162 OR Email us at buyrealfake.documents@yandex.com

    ReplyDelete
  6. Buy best t shirts for men online from Dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete