Q: Can't Remove Cookie

S

Soul

Hi,

I have a C# ASP.Net page allow users to enter their name and will set those
value as cookie after click on "Submit" button. In addition, if user click
on "Change" button, the cookie should be remove. By replacing the same
cookie name with negative expire date, things just work fine.

Unfortunatelly, if I try to use the Cookies.Clear() or Cookies.Remove()
method, the cookie just doesn't remove. Anyone know why?

void ButtonSubmit_Click(object sender, EventArgs e) {
HttpCookie myCookie = new HttpCookie("aCookie");
myCookie.Values["firstname"] = TextBoxFirstname.Text.Trim();
myCookie.Values["lastname"] = TextBoxLastname.Text.Trim();

DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0,0,3,0);
myCookie = dt + ts;

Response.Cookies.Add(myCookie);

Response.Redirect("same.aspx");
}
}
}

// This will work
void ButtonChange_Click(object sender, EventArgs e) {
HttpCookie myCookie = new HttpCookie("aCookie");
myCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(myCookie);
Response.Redirect("same.aspx");
}

// This will NOT work
void ButtonChange_Click(object sender, EventArgs e) {
Response.Cookies.Clear();
Response.Redirect("same.aspx");
}

Thank you.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top