Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Wed, 20-Aug-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
37300
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads
 Article

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

SheoNarayan
Posted by under ASP.NET category on for Advance level | Points: 250 | Views : 15822 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
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.

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,

  1.  btnCookieSet executes SetCookie server side method
  2.  btnCookieGet executes GetCookie server side method
  3.  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.

Page copy protected against web site content infringement by Copyscape
About the Author

SheoNarayan

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Microsoft_MVP,Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com

Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan
Login to vote for this post.
Found interesting? Add this to:


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
Akiii

Posted by: SheoNarayan on: 6/21/2011 | Points: 25

Thanks Akiii,

More will be coming , keep reading and referring friends.

Thanks
Regards

Login to post response

Comment using Facebook(Author doesn't get notification)