Problems setting a cookie that already exists

A

Alan Silver

Hello,

I have discovered that if I try and add a cookie when one by that
already exists, nothing happens. No error, but the cookie is not set to
the new value.

For example (this is running in a DLL, which is why I use
HttpContext)...

HttpCookie cookie = new HttpCookie("fred", "ferret");
DateTime expiryDate = DateTime.Now.AddMonths(1);
cookie.Expires = expiryDate;
HttpContext.Current.Response.Cookies.Add(cookie);

works fine if the cookie "fred" does not already exist, but if it does,
then it is not updated to have the new value.

How do I handle this? I tried...

HttpContext.Current.Response.Cookies["fred"] = cookie;

but that didn't do anything either. Either way, the old value remains in
the cookie.

Any advice appreciated.
 
A

Alan Silver

HttpContext.Current.Response.Cookies.Add(cookie);
works fine if the cookie "fred" does not already exist, but if it does,
then it is not updated to have the new value.

I have since discovered that it was added, but the cookie ended up with
two separate values for "fred", and the code was only able to read the
first.

So, I'm still at a loss as to how to update a cookie value when it
already exists. Any suggestions welcome.
 
A

Alan Silver

So, I'm still at a loss as to how to update a cookie value when it already
exists. Any suggestions welcome.

Well, as no-one has replied yet, I might as well show my latest failure
;-(

The following code is a complete page that demonstrates my equally
complete failure at using cookies. If you save this as an .aspx and load
it in a browser, it tells you it is creating the cookie. If you reload
the page, it tells you it is changing the value. If you reload it again,
it tells you it is removing the cookie.

All of this is exactly what I expected, except that it didn't work. If
you close the browser window at any stage and reload the page, it always
shows the "creating" message. So, it seems that it's not actually
creating the cookie at all.

Please can someone help me here. I've spent hours trying to solve what
should be a really simple problem. TIA


<%@ Page Language="C#" Debug="true" %>
<script runat="server">

void Page_Load(Object o, EventArgs e) {
string cookieName = "fred";
if (Request.Cookies[cookieName] == null) {
// the cookie does not exist. Create it
x.Text = "Creating a new cookie";
HttpCookie cookie = new HttpCookie(cookieName, "newcookie");
cookie.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(cookie);
} else if (Request.Cookies[cookieName].Value == "newcookie") {
// the cookie was created last time this page was called
x.Text = "Changing value from " + Request.Cookies[cookieName].Value + " to oldcookie";
Response.Cookies[cookieName].Value = "oldcookie";
} else {
// the cookie was created before the previous call to this page. Delete it
x.Text = "Removing the cookie whose value is " + Request.Cookies[cookieName].Value;
Response.Cookies.Remove(cookieName);
}
}

</script>

<html>
<body>
<asp:Literal ID="x" EnableViewState="false" RunAt="server" />
</body>
</html>
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top