Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cookies will not expire (noob question)

Reply
Thread Tools

Cookies will not expire (noob question)

 
 
th3dude
Guest
Posts: n/a
 
      07-24-2008
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!


 
Reply With Quote
 
 
 
 
Joern Schou-Rode
Guest
Posts: n/a
 
      07-24-2008
On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <(E-Mail Removed)> wrote:

> 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/
 
Reply With Quote
 
 
 
 
th3dude
Guest
Posts: n/a
 
      07-24-2008
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" <(E-Mail Removed)> wrote:
> On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <(E-Mail Removed)> wrote:
> > * * * * * * 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/


 
Reply With Quote
 
th3dude
Guest
Posts: n/a
 
      07-24-2008
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" <(E-Mail Removed)> wrote:
> On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <(E-Mail Removed)> wrote:
> > * * * * * * 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/


 
Reply With Quote
 
th3dude
Guest
Posts: n/a
 
      07-24-2008
On Jul 24, 12:35*pm, "Joern Schou-Rode" <(E-Mail Removed)> wrote:
> On Thu, 24 Jul 2008 21:16:37 +0200, th3dude <(E-Mail Removed)> wrote:
> > * * * * * * 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies expire immediately, not when set to expire Tongass Park Neighborhood Association, Juneau Alaska ASP General 2 11-24-2009 08:24 PM
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who ASP .Net 7 09-18-2008 07:49 PM
is it possible to know when cookies expire ? Mel HTML 3 03-16-2005 06:31 PM
IE seems to expire all cookies instead of one S. Bourbeau ASP General 0 12-05-2003 06:50 PM
Forms Authentication Cookies Never Expire Joey Powell ASP .Net 2 12-04-2003 08:27 PM



Advertisments