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

11 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. Buy best t shirts for men online from Dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  9. innosilicon a11 pro 8gb 2000mh for sale
    Whatsapp: +86-177-1057-9540
    https://cryptoexperticonminers.org/product/innosilicon-a11-pro-2000-mh-s-ethereum-miner/
    maximum hashrate of 2Gh/s for a power consumption of 2500W. Best and Most powerful Ethereum miner.
    innosilicon a11 pro 8gb 2000mh price
    antminer e9 3gh for sale
    innosilicon a11 pro 8gb 2000mh for sale
    bitmain antminer e9 buy
    innosilicon a11 for sale
    bitmain antminer e9 (3gh)
    innosilicon a11 pro
    innosilicon a11 buy
    bitmain antminer e9 (3gh) for sale
    buy innosilicon a11 pro
    innosilicon a11 pro buy
    a11 pro eth (2000mh) price
    #InnosiliconA11Pro #EthereumMiner #ethereumminerforsale #Innosilicon #InnosiliconA11 #A11Pro #A11Proforsale #InnosiliconA11Pro #ETHminer
    #bitcoin #bitcoinmining #bitcoinnews #bitcointrading #bitcoincash #bitcoinprice #bitcoinusa #bitcoins #bitcoinbrasil #bitcoinasia #bitcoinexchange #bitcoincanada #bitcoinminer #cryptocurrency
    #btc #forex #ethereum #ethereumminer #dogecoin #china
    http://asicscoinminers.com/
    https://nvidiatechgraphics.com/
    https://asicscryptominers.com/
    http://asicsminermarket.com
    http://goldshellcoinminer.com
    http://bitcoin-cashouts.com
    http://asicsbitmainminers.com

    ReplyDelete
  10. https://metalscrapsolution.com/product/red-liquid-mercury-20-20/
    Whatsapp: +1 (443) 252-3065
    buy red liquid mercury, is red snapper high in mercury, liquid red mercury, liquid red mercury buyers, mercury red band, price for red mercury, price of 1 gram of red mercury, price of red mercury, pure red liquid mercury, pure red mercury, red liquid mercury, Red liquid mercury 20/20, red liquid mercury price, what is red mercury liquid used for

    Pure Red liquid mercury for sale online
    Red liquid mercury for sale. Wholesale & retail, discreet/fast shipping worldwide. Metal Scrap Solution Center supplies high grade & perfectly composed Buy Red Liquid Mercury online at cheap & affordable prices.
    We’re a Premium wholesaler & Retailer of Pure Red Liquid Mercury Online. We supply Pure Red Liquid Mercury 20/20 of great quality from USA, China and Denmark. Our Pure Red Liquid Mercury 20/20 for sale online is in high demand by industries worldwide. It is a high grade & perfectly composed Pure Red Liquid Mercury 20/20. Red liquid Mercury is a cherry red liquid produced by irradiating mercury antimony oxide with elemental mercury.We have been specializing in mercury products for decades now, and we truly understand what you need as a customer. After many years of dealing with mercury, our workers are increasing their experience and knowledge.
    https://metalscrapsolution.com/product/buy-caluanie-muelear-oxidize-online/

    ReplyDelete