Jitesh Byahut 4/18/2014 3933

Cookies in C#

Cookies are used to store small amount of data. Best example of cookies is to store login userid.

Create Cookies

You can create a new cookie by using Response.Cookies that contains collection of cookies which is sent from web server to web browser.

// Creating a Cookies

Response.Cookies["user"].Value = "Jitesh";

When you create a cookies which name is already exists on browser, then new cookies value override the previous cookies value. So when you want to edit cookies value, then simply create a new cookies with same name.

Persistent Cookies

Cookies name is case sensitive. When you will close the browser then cookies lost their values. If you want to create cookies that containing values to the specified date no matter how many time browser is closed, on that time need to create persistent cookies that contains expiry date of cookies.

Below example is creating a simple persistent cookies that will expire in next 10 days.

// Creating Persistent Cookies

Response.Cookies["user"].Value = "Jitesh";

Response.Cookies["user"].Expires = DateTime.Now.AddDays(10);

Reading Cookies values

You can retrieve cookies values by their name, but remember that before retrieving cookies value, first check that cookies is exists or not otherwise it will throw an exception like : System.NullReferenceException: Object reference not set to an instance of an object.

// Reading Cookies Value

if (Request.Cookies["user"] != null)

{

     string name = Request.Cookies["user"].Value;

     Response.Write("Cookies Value - " + name);

}

        // Output

        // Cookies Value - Jitesh

Reading all cookies

Retrieve all cookies which are stored on browser.

// Reading Cookies Value

for (int i = 0; i < Request.Cookies.Count; i++)

{

     Response.Write(Request.Cookies[i].Name +" - " + Request.Cookies[i].Value + "</br>");

}

        // Output

 

        // empusername - jitesh

        // emplastlogin - 01-11-2013 22:44:55

        // empid - 4

        // ASP.NET_SessionId - 344pjbdu3u2abdy5vxljnano

        // user - Jitesh

Multi values cookies

According to cookies specifications, browsers should not have more than 20 cookies from a single domain. We can remove this limitations by using multivalues cookies. A multivalues cookies is a single cookies with many subkeys. A single domain cannot contain more than 4096 bytes in cookies.

// Creating Multivalue Cookies

Response.Cookies["user"]["name"] = "Jitesh";

Response.Cookies["user"]["email"] = "jitesh@asphelps.com";

Reading multivalue cookies's values

// Reading multivalue cookies values

 

if (Request.Cookies["user"] != null)

{

    string name = Request.Cookies["user"]["name"];

    string email = Request.Cookies["user"]["email"];

 

    Response.Write("Cookies Name - " + Request.Cookies["user"]["name"] + "</br>");

    Response.Write("Cookies EmailID - " + Request.Cookies["user"]["email"]);

}

        // Output

        // Cookies Name - Jitesh

        // Cookies EmailID - jitesh@asphelps.com

Deleting cookies

// Delete a Cookies 

Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1);

Deleting all cookies

// Deleting all cookies from browser

 

foreach (string cookie in Request.Cookies.AllKeys)

{

   Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);

}



Please give your feedback for improving this page