Cookies will not expire (noob question)

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!
 
J

Joern Schou-Rode

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 :)
 
T

th3dude

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 :)
 
T

th3dude

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 :)
 
T

th3dude

            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!
 

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,039
Latest member
CasimiraVa

Latest Threads

Top