According to the Wikipedia, cookie is a small flat baked pastry. According to my definition it is delicious. But in web development world cookie represents a small text file which is saved either resides in the user’s session or the machine and stores the personalized information. Cookies can be turned off, meaning the browser will not accept any cookies from any website. The real question is how do we find out if the browser is accepting cookies or not. In this article I will demonstrate how to check if the browser is configured to accept cookies.

 

Introduction:

 

According to the Wikipedia, cookie is a small flat baked pastry. According to my definition it is delicious. But in web development world cookie represents a small text file which is saved either resides in the user’s session or the machine and stores the personalized information.

 

Cookies can be turned off, meaning the browser will not accept any cookies from any website. The real question is how do we find out if the browser is accepting cookies or not. In this article I will demonstrate how to check if the browser is configured to accept cookies.

 

The Myth about Request.Browser.Cookies:

 

I see a lot of questions on www.asp.net forums where the users asked that how can I check if the browser is able to handle the cookies and the site visitor is not blocking the cookies. This is a two part question and usually they get the answer of the first part by simply using the Request.Browser.Cookies property. The Request.Browser.Cookies will return a boolean value indicating that if the browser has the capability of handling cookies. But, it will not tell you that if the visitor has configured the browser to block cookies. So, even if the site visitor has turned off accepting cookies the Request.Browser.Cookies will still return true since, the browser has capability of accepting cookies it is just that user has turned off that feature.

 

So, How Can This Be Done:

 

A simple, idea is to create the cookie and read the cookie. If while reading the cookie it returns null then it means that cookie was not written and the user has turned off the cookie accepting feature.

 

The Implementation:

 

The first step is to create some dummy class level strings for the cookie name and the value.

 

private string cookieName = "cookieName";

private string cookieValue = "cookieValue";

 

The next step is to create the cookie. Take a look at the code below which, demonstrates how to create a cookie.

 

private void CreateCookie()

    {               /*

         * Please note that even if the browser does not accept cookies creating and attaching the

         * cookie to the response will not throw any exception

         *  Request.Browser.Cookies only tells that if the browser is capable of creating cookies. But it does not tell that if the cookies

                are being accepted by the user.*/

 

        // create a cookie

        // You can encrypt the cookie

        HttpCookie cookie = new HttpCookie(cookieName, cookieValue);

 

        // The above cookie is a session cookie meaning that once, you close the browser the cookie is gone.

        // You can use cookie.Expires to set the expiration time on the cookie in which case it will last long :)

 

        // add the cookie to the response

 

        Response.Cookies.Add(cookie);

    }

 

I have commented the code so, you will have a good idea what is going on. The cookie created in the code above is known as Session Cookie. The reason is because I have not set the expiration date and hence it will expire when the user closes the browser. If you need to create persistence cookie then you should use the cookie.Expires property and set the expiration date and time.

 

The next step is to read the cookie. Check out the following code:

 

private bool DoesUserBrowserAcceptCookies()

    { 

        bool result = false;

 

        // Now read the cookie back

        if (Request.Cookies[cookieName] != null)

        {

            if (Request.Cookies[cookieName].Name.Equals(cookieName))

                result = true;

 

        }

 

        return result;

   

    }

 

The code above simply tries to read the cookie. If the cookie is present then it means that the user has configured the browser to accept cookies else he has not.

 

Conclusion:

 

In this article I demonstrated how to check if the user has configured the browser to accept cookies.

 

I hope you liked this article, happy programming!