cookie trouble

K

Kevin Blount

I've created a quick set of test scripts, and I'm finding that cookies
don't work as I would expect(i.e. the same as they do in original ASP.
In original ASP I could set a cookie on one page, set another value to
that cookie on a second page, then read them both back on a third page,
but in .NET the second page overwrites the first.

here are my three .NET test pages:

setCookie1.aspx:
Response.Cookies["Communities"]["LoggedIn"] = "true";
Response.Cookies["Communities"]["member_id"] = "120";
Response.Cookies["Communities"].Expires = DateTime.Now.AddYears(30);

setCookie2.aspx:
Response.Cookies["Communities"]["banner"] = "1";
Response.Cookies["Communities"].Expires = DateTime.Now.AddYears(30);

viewCookies.aspx:
System.Text.StringBuilder output = new System.Text.StringBuilder();
HttpCookie aCookie;
for(int i=0; i<Request.Cookies.Count; i++)
{
aCookie = Request.Cookies;
output.Append("Cookie name = " + Server.HtmlEncode(aCookie.Name) +
"<br />");
output.Append("Cookie value = " + Server.HtmlEncode(aCookie.Value)
+ "<br /><br />");
}
Response.Write(output.ToString());


am I doing something wrong? In the real world here what I need to do:

I have a template that reads from the "Communities" cookie for the
value of the "LoggedIn" name, and if it's "true" does some database
work using the value of "member_id".

Later in this template I look for the value of "banner" and display a
graphic based on that value. I then increment the value and write it
back to the cookie.

It's at this point that "LoggedIn" and "member_id" are removed from the
cookie, and "banner" is added in their place. What I really would like
is for "banner" to be appended to the "Communities" cookie, and for all
three name-value pairs to be available, as I can do is original ASP.

Any ideas?
 
S

Scott M.

That's because you are not using them correctly. Use HTTPCookie everywhere.
Don't use Response & Request.Cookie.
 
K

Kevin Blount

Arh.. thanks for the heads up. I'm new at this and figured Microsoft's
website knew what it was talking about hehe

I'll check out HTTPCookie and see what gives.

Many thanks

Kevin
 
K

Kevin Blount

I did the research, but I still cannot get a working test.

The research does show that Response.Cookie.Add is the proper code, but
if I use this on two pages, the second pages overwrite the cookie
written on the first.

Again, I want pag 1 to set a "Communities" cookie with "loggedin=true"
and "member_id=120", and page2 to append to that "Communities" cookie
"banner=1", then for page 3 to read the cookie collection "Communities"
and show a value of "loggedin=true&member_id=120&banner=1"

Doesn't anyone know how? Sample code or URLs would be greatly
apprecaited.

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top