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

12 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. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    Webseiten Gestaltung L├╝denscheid

    ReplyDelete

  6. 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
  7. Great Article
    Cloud Computing Projects




    Networking Projects



    Final Year

    Projects for CSE





    JavaScript Training in Chennai



    JavaScript

    Training in Chennai




    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training

    ReplyDelete
  8. This blog has amazing information regarding this topic.

    Ambien 10mg is an online medical store and online pharmacy that provides its customers with the best experience in buying medicines online.
    Visit Website: Best Online Pharmacy in US

    Order Xanax from here at 15% OFF - Buy xanax Online

    Order Adderall from here at 15% OFF - Buy Adderall Online

    Order Ambien from here at 15% OFF - Buy Ambien Online

    Order Tramadol from here at 15% OFF - Buy Tramadol Online

    ReplyDelete
  9. Very Nice information you have provided..
    Adderall is synthetic drug that stimulates the sympathetic and central nervous systems, used to treat attention deficit disorder and narcolepsy.

    Buy Adderall Online at 20% OFF

    Buy Alprazolam Online at 20% OFF

    Buy Ambien Online at 20% OFF

    Buy Dilaudid Online at 20% OFF

    ReplyDelete
  10. USA Pain Pharma assists you with giving the information about physician recommended drugs that are endorsed to treat different sorts of illnesses. We provide our services in the United States, USA. To submit your request as for the time being conveyance, you can pick your items and spot them anytime and get it conveyed the following day at your doorstep.

    Here are some medicines USA Pain Pharma provides with online prescription, they give information about medicine you want to buy. Visit their website for details


    buy xanax online
    buy adderall online
    buy ambein online
    buy tramadol online
    buy soma online
    buy vicodin online
    buy Oxycontin Online
    Buy Codeine Online
    Buy Carisoprodol Online

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

    ReplyDelete