How to read-write multi-valued cookies in ASP.NET?

Posted by in ASP.NET category on for Advance level | Points: 250 | Views : 29776 red flag
Rating: 5 out of 5  
 1 vote(s)

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.


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.


<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,

  1.  btnCookieSet executes SetCookie server side method
  2.  btnCookieGet executes GetCookie server side method
  3.  btnRemoveCookie executes RemoveCookie server side method


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();


cookieValue = Request.Cookies["MyCookie"]["My2"].ToString();


// 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.


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.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 6/21/2011 | Points: 25
Hi Sir,
It great to read all the essentials of cookies that we implement in real life scenarios......
Your articles are invaluable......

Looking forward for more articles related to this matter....
Thanks and Regards
Posted by: SheoNarayan on: 6/21/2011 | Points: 25
Thanks Akiii,

More will be coming , keep reading and referring friends.

Posted by: Siddydv27 on: 11/15/2017 | Points: 25
is is so play the quit the text twist 2 again the play.
Posted by: Siddydv27 on: 11/15/2017 | Points: 25
Thanks for the play game [url=]text twist free online[/url] so more fast play.
Posted by: Siddydv27 on: 2/28/2018 | Points: 25
So thanks for the play online game.

Login to post response

Comment using Facebook(Author doesn't get notification)