In this article we are going to learn how to read and write multi-valued cookies in ASP.NET. Multi-valued cookies is just like a normal cookies however it gives us flexibility to store multiple data in a single cookies using subkeys.
Introduction
A Cookie is a small amount of text that attached to the requet and response in between browser and the server. This small amount of text can be read by the application whenever user browse the it. Please note that cookie is not a good medium to store the confidential data as it is stored into the user browser.
ASPX PAGE
<asp:Button ID="btnCookieSet" runat="server" Text="Set Cookie" OnClick="SetCookie" />
<asp:Button ID="btnCookieGet" runat="server" Text="Get Cookie" OnClick="GetCookie" />
<asp:Button ID="btnRemoveCookie" runat="server" Text="Remove Cookie" OnClick="RemoveCookie" />
In the above code snippet we have three buttons,
- btnCookieSet executes SetCookie server side method
- btnCookieGet executes GetCookie server side method
- btnRemoveCookie executes RemoveCookie server side method
CODE BEHIND
protected void SetCookie(object sender, EventArgs e)
{
Response.Cookies["MyCookie"]["My1"] = "My cookie value 1";
Response.Cookies["MyCookie"]["My2"] = "My cookie value 2";
Response.Cookies["MyCookie"].Expires = DateTime.Now.AddSeconds(10);
// the above code can be replaced with
//HttpCookie cookie = new HttpCookie("MyCookie");
//cookie.Values["My1"] = "My cookie value 1";
//cookie.Values["My2"] = "My cookie value 2";
//cookie.Expires = DateTime.Now.AddSeconds(10);
//Response.Cookies.Add(cookie);
}
protected void GetCookie(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] != null)
{
string cookieValue = Request.Cookies["MyCookie"]["My1"].ToString();
Response.Write(cookieValue);
cookieValue = Request.Cookies["MyCookie"]["My2"].ToString();
Response.Write(cookieValue);
// Above code can be replaced with
//HttpCookie cookie = Request.Cookies["MyCookie"];
//Response.Write(cookie.Values["My1"]);
//Response.Write(cookie.Values["My2"]);
}
}
protected void RemoveCookie(object sender, EventArgs e)
{
Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(-1);
}
Get video tutorials of hundreds of ASP.NET Tips and Tricks.
SetCookie method
This method creates a cookie called “MyCookie” with subkey as “My1” and “My2”. As “My1” and “My2” is the subkeys of “MyCookie” so setting the expiry property for this sets the epiration of both “My1” and “My2”.
The same can be done using the HttpCookie object as well, it has been shown in the above code snippet.
GetCookie method
In this method, first we are checking for “MyCookie”, if it is not null then fetching both cookie subkeys and writing on the page using Response.Write method.
RemoveCookie method
To remove the cookie, we have used the Expires property, setting Expires property for the main key applies for both subkyes.
OUTPUT

In case you have missed earlier articles series on cookies, click here. In the next article, we shall learn how to limit the scope of the cookies to a particular folder.
Thanks for reading.