Multiple valued cookie or Sub keys in cookie


Can we set multiple values to a same cookie??

edit | flag
William Patrick
Asked on: Jun 16, 2011 at 5:19AM

1 Answers


Yes, you can set multiple values in a cookie. Try the code below to do this.

Response.Cookies("userInfo")("userName") = "multiple cookie"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1);

You can also store multiple name-value pairs in a single cookie. The name-value pairs are referred to as "keys" or "subkeys," depending on what you are reading. (Subkeys are laid out much like a query string in a URL, should you happen to be familiar with that structure.) As an example, instead of creating two separate cookies named "userName" and "lastVisit," you could create a single cookie named "userInfo" that has the subkeys "userName" and "lastVisit."

There are a couple of reasons to use subkeys instead of separate cookies. Obviously, it is tidy to put related or similar information into a single cookie. In addition, because all the information is in a single cookie, cookie attributes such as expiration apply to all the information. (Conversely, of course, if you want to assign different expiration dates to different types of information, you should store the information in separate cookies.)

Check MSDN for more info

edit | flag
Gokul A
Answered on: Jun 16, 2011 at 7:37PM

Post your Answer


Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags