Create Multivalued Cookies in C#


In general Browser will not save more than 20 cookies from single domain. That means you can create only twenty cookies per each web application, this limiting the cookie usage. We can overcome this by using Multivalued cookies in C#.

Multivalued cookie is nothing but a single cookie which contains subkeys. There is no limitation of creating subkeys. In this article we discuss about how to create Multivalued cookies in C#.

Open Microsoft Visual Studio 2013 => Create New Project => Create New Web Application

Create Multivalued cookie as shown below.

Response.Cookies["Employee"]["Id"] = "1001";

Response.Cookies["Employee"]["Name"] = "John";

Response.Cookies["Employee"].Expires = DateTime.MaxValue;

 

As shown above we create multivalued cookie Employee with subkeys Id & Name. You can add any number of subkeys to multivalued cookie. We provided the Expire date as maximum date.

We can get the subkey value from the multivalued cookie as shown below.

string sId = Response.Cookies["Employee"]["Id"];

string sName = Response.Cookies["Employee"]["Name"];

 

We can check whether cookie is multivalued cookie or normal key by using HttpCookie.HasKeys property. If HttpCookie.HasKeys returns true that cookie is multivalued cookie else it is normal cookie.