7

I am using ASP.NET and C#.

I must read a cookie called "TheCookie".............

TheCookie have about 3 values in them. Cookie1, Cookie2 and Cookie3.

How would i get the value in code to read Cookie2's value inside "TheCookie"?

This is how i would read when a cookie only have 1 value, but i dont know what to do when there are multiple vales in the cookie.......... Code for VB.NET

Dim userCookie As HttpCookie
userCookie = Request.Cookies("UserEmail")

Thanks in advance!

1
  • You need to explain this furthur, I think. A cookie can only have one value. You could have set mutiple cookies or set a single cookies value as an object with multipole properties? Commented Oct 14, 2009 at 8:47

3 Answers 3

14

You set them via

(C#)

Response.Cookies["TheCookie"]["Cookie1"] = "Hello World";

(VB)

Response.Cookies("TheCookie")("Cookie1") = "Hello World"

and read them like so

(C#)

string myValue = Request.Cookies["TheCookie"]["Cookie1"];

(VB)

Dim myValue As String
myValue = Request.Cookies("TheCookie")("Cookie1")
Sign up to request clarification or add additional context in comments.

Comments

2
Request.Cookies.Get("TheCookie").Values.Get("Cookie1")
Request.Cookies.Get("TheCookie").Values.Get("Cookie2")
Request.Cookies.Get("TheCookie").Values.Get("Cookie3")

C# syntax, sorry!

Comments

0

we can save it by passing as dictionary object key value pair as below.

 HttpCookie hc = new HttpCookie(cookieName);
 foreach (KeyValuePair<string, string> val in dic)
 {
   hc[val.Key] = val.Value;
  }
 hc.Expires = DateTime.Now.Add(TimeSpan.FromHours(20000));
 GetHttpResponse().Cookies.Add(hc);

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.