Connecting Tech Pros Worldwide Forums | Help | Site Map

Can't remove cookies!

Stephane
Guest
 
Posts: n/a
#1: Nov 19 '05
Hi,

I have a login page where if the user wants his access codes to be saved are
set into a cookie. In the logout page, I want to delete those cookies. I
tried this and this is not working at all:

if (Request.Cookies[COOKIE_USER] != null
&& Request.Cookies[admin.COOKIE_PSWD] != null)
{
Response.Cookies[COOKIE_USER].Value = null;
Response.Cookies[COOKIE_PSWD].Value = null;
Response.Cookies.Remove(COOKIE_USER);
Response.Cookies.Remove(COOKIE_PSWD);
}

If I do a response.write like this right after or on another page, it shows
that the cookies are still set:

Response.Write((Request.Cookies[COOKIE_USER] == null) + " " +
Request.Cookies[COOKIE_USER].Value + " ");
Response.Write((Request.Cookies[COOKIE_PSWD] == null) + " " +
Request.Cookies[COOKIE_PSWD].Value);

Why is this not working? How do we remove user's cookie?

Thanks

Stephane


Chris R. Timmons
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Can't remove cookies!


"=?Utf-8?B?U3RlcGhhbmU=?=" <Stephane@discussions.microsoft.com>
wrote in news:9E0FDC2B-429E-4868-98A4-47625C42DAF0@microsoft.com:
[color=blue]
> Hi,
>
> I have a login page where if the user wants his access codes to
> be saved are set into a cookie. In the logout page, I want to
> delete those cookies. I tried this and this is not working at
> all:
>
> if (Request.Cookies[COOKIE_USER] != null
> && Request.Cookies[admin.COOKIE_PSWD] != null)
> {
> Response.Cookies[COOKIE_USER].Value = null;
> Response.Cookies[COOKIE_PSWD].Value = null;
> Response.Cookies.Remove(COOKIE_USER);
> Response.Cookies.Remove(COOKIE_PSWD);
> }
>
> If I do a response.write like this right after or on another
> page, it shows that the cookies are still set:
>
> Response.Write((Request.Cookies[COOKIE_USER] == null) + " " +
> Request.Cookies[COOKIE_USER].Value + " ");
> Response.Write((Request.Cookies[COOKIE_PSWD] == null) + " " +
> Request.Cookies[COOKIE_PSWD].Value);
>
> Why is this not working? How do we remove user's cookie?[/color]

Stephane,

Set the cookie's expiration date to sometime in the past. When it is
sent back to the browser, the browser will look at the cookie's date
and determine it has expired and delete it. For example:

Response.Cookies[COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
Response.Cookies[COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Stephane
Guest
 
Posts: n/a
#3: Nov 19 '05

re: Can't remove cookies!


Hi,

I tried this and the damned cookie is still there!

Response.Cookies[admin.COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
Response.Cookies[admin.COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
Response.Cookies[admin.COOKIE_USER].Value = null;
Response.Cookies[admin.COOKIE_PSWD].Value = null;
Response.Cookies.Remove(admin.COOKIE_USER);
Response.Cookies.Remove(admin.COOKIE_PSWD);

Maybe it's the way I set the cookie?

HttpCookie cookieUsr = new HttpCookie(COOKIE_USER, username);
cookieUsr.Expires = DateTime.Now.AddDays(1000);
HttpCookie cookiePwd = new HttpCookie(COOKIE_PSWD, encryptedValue);
cookiePwd.Expires = DateTime.Now.AddDays(1000);
Response.SetCookie(cookieUsr);
Response.SetCookie(cookiePwd);

Thanks for any help!

Stephane


"Chris R. Timmons" wrote:
[color=blue]
> "=?Utf-8?B?U3RlcGhhbmU=?=" <Stephane@discussions.microsoft.com>
> wrote in news:9E0FDC2B-429E-4868-98A4-47625C42DAF0@microsoft.com:
>[color=green]
> > Hi,
> >
> > I have a login page where if the user wants his access codes to
> > be saved are set into a cookie. In the logout page, I want to
> > delete those cookies. I tried this and this is not working at
> > all:
> >
> > if (Request.Cookies[COOKIE_USER] != null
> > && Request.Cookies[admin.COOKIE_PSWD] != null)
> > {
> > Response.Cookies[COOKIE_USER].Value = null;
> > Response.Cookies[COOKIE_PSWD].Value = null;
> > Response.Cookies.Remove(COOKIE_USER);
> > Response.Cookies.Remove(COOKIE_PSWD);
> > }
> >
> > If I do a response.write like this right after or on another
> > page, it shows that the cookies are still set:
> >
> > Response.Write((Request.Cookies[COOKIE_USER] == null) + " " +
> > Request.Cookies[COOKIE_USER].Value + " ");
> > Response.Write((Request.Cookies[COOKIE_PSWD] == null) + " " +
> > Request.Cookies[COOKIE_PSWD].Value);
> >
> > Why is this not working? How do we remove user's cookie?[/color]
>
> Stephane,
>
> Set the cookie's expiration date to sometime in the past. When it is
> sent back to the browser, the browser will look at the cookie's date
> and determine it has expired and delete it. For example:
>
> Response.Cookies[COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
> Response.Cookies[COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
>
> --
> Hope this helps.
>
> Chris.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/
>[/color]
vMike
Guest
 
Posts: n/a
#4: Nov 19 '05

re: Can't remove cookies!



Try this (I only know vb so I may not have all the syntax correct) Also,
you want to make sure the request.cookie exists first or it will throw an
error.
if not request.cookies[admin.COOKIE_USER] is nothing then
Request.Cookies[admin.COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
Request.Cookies[admin.COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
Reponse.Cookies.add(Request.Cookies[admin.COOKIE_USER])
Response.Cookies.add(Request.Cookies[admin.COOKIE_PSWD])

Basically you are adding an expired cookie. From my experience remove cookie
does not work I saw an explanation of why somewhere but I can't remember
where.


vMike
Guest
 
Posts: n/a
#5: Nov 19 '05

re: Can't remove cookies!



"vMike" <MicZhaYel.GeoZrgeY@noYandZ.geZwaYrrenZ.com> wrote in message
news:cq7so2$o0m$1@ngspool-d02.news.aol.com...[color=blue]
>
> Try this (I only know vb so I may not have all the syntax correct) Also,
> you want to make sure the request.cookie exists first or it will throw an
> error.
> if not request.cookies[admin.COOKIE_USER] is nothing then
> Request.Cookies[admin.COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
> Request.Cookies[admin.COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
> Reponse.Cookies.add(Request.Cookies[admin.COOKIE_USER])
> Response.Cookies.add(Request.Cookies[admin.COOKIE_PSWD])
>
> Basically you are adding an expired cookie. From my experience remove[/color]
cookie[color=blue]
> does not work I saw an explanation of why somewhere but I can't remember
> where.
>
>[/color]

BTW I don't think the cookie is actually removed, it is just expired. I
don't think you can erase the cookie from the client. Others may know if
that is possible.


vMike
Guest
 
Posts: n/a
#6: Nov 19 '05

re: Can't remove cookies!



"Stephane" <Stephane@discussions.microsoft.com> wrote in message
news:CF99C0E3-BF61-4631-B5C0-8D185589F5BD@microsoft.com...[color=blue]
> Hi,
>
> I tried this and the damned cookie is still there!
>
> Response.Cookies[admin.COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
> Response.Cookies[admin.COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
> Response.Cookies[admin.COOKIE_USER].Value = null;
> Response.Cookies[admin.COOKIE_PSWD].Value = null;
> Response.Cookies.Remove(admin.COOKIE_USER);
> Response.Cookies.Remove(admin.COOKIE_PSWD);
>
> Maybe it's the way I set the cookie?
>
> HttpCookie cookieUsr = new HttpCookie(COOKIE_USER, username);
> cookieUsr.Expires = DateTime.Now.AddDays(1000);
> HttpCookie cookiePwd = new HttpCookie(COOKIE_PSWD, encryptedValue);
> cookiePwd.Expires = DateTime.Now.AddDays(1000);
> Response.SetCookie(cookieUsr);
> Response.SetCookie(cookiePwd);
>[/color]
This article may help http://www.codeproject.com/aspnet/aspnetcookies.asp


Stephane
Guest
 
Posts: n/a
#7: Nov 19 '05

re: Can't remove cookies!


I finally had it to work... I remove it from the request too.

Here's my code:

HttpCookie c1 = Request.Cookies[admin.COOKIE_USER];
HttpCookie c2 = Request.Cookies[admin.COOKIE_PSWD];
Request.Cookies.Remove(admin.COOKIE_USER);
Request.Cookies.Remove(admin.COOKIE_PSWD);
c1.Expires = DateTime.Now.AddDays(-10);
c2.Expires = DateTime.Now.AddDays(-10);
c1.Value = null;
c2.Value = null;
Response.SetCookie(c1);
Response.SetCookie(c2);

Stephane

"vMike" wrote:
[color=blue]
>
> "Stephane" <Stephane@discussions.microsoft.com> wrote in message
> news:CF99C0E3-BF61-4631-B5C0-8D185589F5BD@microsoft.com...[color=green]
> > Hi,
> >
> > I tried this and the damned cookie is still there!
> >
> > Response.Cookies[admin.COOKIE_USER].Expires = DateTime.Now.AddDays(-10);
> > Response.Cookies[admin.COOKIE_PSWD].Expires = DateTime.Now.AddDays(-10);
> > Response.Cookies[admin.COOKIE_USER].Value = null;
> > Response.Cookies[admin.COOKIE_PSWD].Value = null;
> > Response.Cookies.Remove(admin.COOKIE_USER);
> > Response.Cookies.Remove(admin.COOKIE_PSWD);
> >
> > Maybe it's the way I set the cookie?
> >
> > HttpCookie cookieUsr = new HttpCookie(COOKIE_USER, username);
> > cookieUsr.Expires = DateTime.Now.AddDays(1000);
> > HttpCookie cookiePwd = new HttpCookie(COOKIE_PSWD, encryptedValue);
> > cookiePwd.Expires = DateTime.Now.AddDays(1000);
> > Response.SetCookie(cookieUsr);
> > Response.SetCookie(cookiePwd);
> >[/color]
> This article may help http://www.codeproject.com/aspnet/aspnetcookies.asp
>
>
>[/color]
Closed Thread