Cookies will not expire (noob question)

  • Thread starter Thread starter th3dude
  • Start date Start date
T

th3dude

This seems like an easy one, but i'm stuck on getting rid of a cookie
and cannot seem to make it happen.

I'm creating a cookie like so:


private void createCookie()
{
HttpCookie cookie = Request.Cookies["myCookie"];
if (cookie == null)
{
cookie = new HttpCookie("myCookie");
}


cookie["Name"] = "myName";
cookie["PW"] = "myPw";


cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);


lbl_welcome.Text = "<b>Cookie Created.</b>";
lbl_welcome.Text = "Logged in As: " + cookie["Name"] + "
Pw: " + cookie["PW"];


}


Then upon clicking a button i'm trying to get rid of the cookie (but
it does not go away):


private void doLogout()
{


HttpCookie cookie = Request.Cookies["myCookie"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddMinutes(-1);
}


}


What am i missing here, seems fairly simple?


Thanks for any help!
 
HttpCookie cookie = Request.Cookies["myCookie"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddMinutes(-1);
}

In the code quoted above, you read a cookie from the request (the input
stream) and you modify it, but you do not write the changes to the
response (the output stream). I believe you should be able to do something
like the following _instead_ of what you have above:

Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);

Somewhat off-topic: from the code you are showing, it seems that you are
implementing some kind of authentication/login scheme. If this is the
case, you might be happy to know that ASP.NET has several authentication
implementations built right in - the one called "forms authentication"
might just save you a lot of work :-)
 
Hi Joern,

You're tip fixed my cookie issue :)

Many thanks also for the advice on using built-in authentication, i'll
take of the help i can get.

Cheers!



            HttpCookie cookie = Request.Cookies["myCookie"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddMinutes(-1);
            }

In the code quoted above, you read a cookie from the request (the input  
stream) and you modify it, but you do not write the changes to the  
response (the output stream). I believe you should be able to do something  
like the following _instead_ of what you have above:

Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);

Somewhat off-topic: from the code you are showing, it seems that you are  
implementing some kind of authentication/login scheme. If this is the  
case, you might be happy to know that ASP.NET has several authentication  
implementations built right in - the one called "forms authentication"  
might just save you a lot of work :-)
 
Hi Joern,

Your tip fixed my cookie issue :)


Many thanks also for the advice on using built-in authentication,
i'll
take of the help i can get.


Cheers!


            HttpCookie cookie = Request.Cookies["myCookie"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddMinutes(-1);
            }

In the code quoted above, you read a cookie from the request (the input  
stream) and you modify it, but you do not write the changes to the  
response (the output stream). I believe you should be able to do something  
like the following _instead_ of what you have above:

Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);

Somewhat off-topic: from the code you are showing, it seems that you are  
implementing some kind of authentication/login scheme. If this is the  
case, you might be happy to know that ASP.NET has several authentication  
implementations built right in - the one called "forms authentication"  
might just save you a lot of work :-)
 
            HttpCookie cookie = Request.Cookies["myCookie"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddMinutes(-1);
            }

In the code quoted above, you read a cookie from the request (the input  
stream) and you modify it, but you do not write the changes to the  
response (the output stream). I believe you should be able to do something  
like the following _instead_ of what you have above:

Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);

Somewhat off-topic: from the code you are showing, it seems that you are  
implementing some kind of authentication/login scheme. If this is the  
case, you might be happy to know that ASP.NET has several authentication  
implementations built right in - the one called "forms authentication"  
might just save you a lot of work :-)

Hi Joern,

Your tip fixed my cookie issue :)


Many thanks also for the advice on using built-in authentication,
i'll take all of the help i can get.


Cheers!
 
Back
Top