checking for an existing cookie

Posted on 2011-10-31
Last Modified: 2012-05-12

I have the following code:
   if (Response.Cookies["888"] == null)

                aCookie = new HttpCookie("888");
                aCookie.Value = System.Guid.NewGuid().ToString();                
                aCookie.Expires = DateTime.Now.AddDays(7);

I've just used a dummy name 888 for now but I'm trying to check to see if a cookie exists and if not create it.

However the code above always gets stepped over, even when the cookie shouldn't exist.

Am I doing the correct check?

Question by:scm0sml
    1 Comment
    LVL 74

    Accepted Solution

    Shouldn't you be checking Request.Cookies rather than Response.Cookies? Add to the Response; read from the Request.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now