Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cookie Anomaly

Thread Tools

Cookie Anomaly

Posts: n/a

I'm creating a cookie on my site, and adding values to it, namely a userid
and a groupid:

oCookie.Values["groupid"] = siteID.ToString();

oCookie.Values["userid"] = siteID.ToString();

where oCookie is created like this:

HttpCookie oCookie = HttpContext.Current.Request.Cookies[CookieName];
if (oCookie == null)
oCookie = new HttpCookie(CookieName);
return oCookie;

My problem is this:
When I add the groupID, and then call Cookies.Set, a new Cookie with the
same name is added to the Cookies Collection, instead of just setting the
groupid value. ie, it creates two cookies, both with the groupid key and
value. If I omit the Cookies.Set, the cookie is created only once, and
everything is fine......

except, when I do this with the userID, if I don't call Cookies.Set, the
userID is not set to the cookie. If I do, the value/name pair is added to
the existing cookie, like it should be.

So, for clarity, these are the three scenarios:
1. Add groupID, call Cookies.Set, Add userID call Cookies.Set = two Cookies
created, both with the correct name value pairs.
2. Add groupID, DON'T call Cookies.Set, Add userID call Cookies.Set = one
Cookie created, both with the correct name value pairs. (DESIRED RESULT)
3. Add groupID, call Cookies.Set, Add userID DON'T call Cookies.Set = one
Cookie created, userID name/value pair not created.

How much sense does this make?!?

The setting of the groupid takes place before the userid, at different
points in the application. oCookie always exists when the setting of these
values take place. The cookie's a session cookie ie. doesn't persist.

Any insight would be greatly appreciated.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is different between Request.Cookie and Response.Cookie ad ASP .Net 2 01-27-2006 12:54 PM
Cookie Question (IP as domain and cookie file location) =?Utf-8?B?UGF1bA==?= ASP .Net 1 01-10-2006 08:37 PM
Any downsides to cookie assignment inside custom class using HttpContext.Current? ASP.NET 2.0 cookie fix? ASP .Net 2 08-17-2005 06:43 AM
Cookie and Session Cookie Questions. Shapper ASP .Net 1 04-27-2005 11:20 AM
Session cookie? Browser instance cookie? Ben ASP .Net 3 06-03-2004 03:41 AM