Bob Jones
Bob Jones

Reputation: 2048

Cookies that won't delete

I feel like I have been eaten by the cookie monster. I delete cookies and they come right back. On logout, I execute this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        DeleteCookie("UserId");
        DeleteCookie("UserName");

        Session.Abandon();
        GoToPublicHomePage();
    }

    private void DeleteCookie(string name)
    {
        if (Request.Cookies[name] != null)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        }
    }

Just before the "GoToPublicHome()" call, I check the Request.Cookies and the UserId and UserName cookies are in Response.

In my Global.asax --> Session_Start code which executes immediately after the GoToPublicHome call, they have come back... like a bad penny.

Upvotes: 1

Views: 703

Answers (2)

toby
toby

Reputation: 902

How about Response.Cookies.Remove(name)?

Upvotes: 0

JScoobyCed
JScoobyCed

Reputation: 10413

The code you write instructs .Net to create a new HttpCookie, not to create "only if not exists". On the MSDN page there is an example: http://msdn.microsoft.com/en-us/library/system.web.httpcookie.aspx

Upvotes: 1

Related Questions