Cookie created twice

A

Ash

Hi

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

Function1:
oCookie.Values["groupid"] = siteID.ToString();
HttpContext.Current.Response.Cookies.Set(oCookie);

Function2:
oCookie.Values["userid"] = siteID.ToString();
HttpContext.Current.Response.Cookies.Set(oCookie);

where oCookie is created like this:

HttpCookie oCookie = HttpContext.Current.Request.Cookies[CookieName];
if (oCookie == null)
{
oCookie = new HttpCookie(CookieName);
HttpContext.Current.Response.Cookies.Add(oCookie);
}
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.

Ash
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top