By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

Cookies will not expire (noob question)

P: n/a
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!
Jul 24 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gn*****@gmail.comwrote:
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 :-)

--
Joern Schou-Rode
http://malamute.dk/
Jul 24 '08 #2

P: n/a
On Jul 24, 12:35*pm, "Joern Schou-Rode" <j...@malamute.dkwrote:
On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail.comwrote:
* * * * * * 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 :-)

--
Joern Schou-Rodehttp://malamute.dk/
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!
Jul 24 '08 #3

P: n/a
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!

On Jul 24, 12:35*pm, "Joern Schou-Rode" <j...@malamute.dkwrote:
On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail.comwrote:
* * * * * * 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 :-)

--
Joern Schou-Rodehttp://malamute.dk/
Jul 24 '08 #4

P: n/a
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!
On Jul 24, 12:35*pm, "Joern Schou-Rode" <j...@malamute.dkwrote:
On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <gnot...@gmail.comwrote:
* * * * * * 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 :-)

--
Joern Schou-Rodehttp://malamute.dk/
Jul 24 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.