Register  |  Login

Question Details    

   Question

Time: 07:53 - Jul 19, 2006     Asked by: dustPuppy      Status: Answered      Points: 75   

asp.net removing cookies?

Hi all,

I have been trying to remove some cookies that I set, but without luck. I'm using asp.net 1.1 and c#.
First I have the problem that I can access the cookies collection two ways: this.response.cookies and this.request.cookies, but which should I use?
I did use request and then just set the cookie to an empty string, but the idea was to remove it completely.
When i tried to use this.Request.Cookies.Remove("xxx"); it was removed in the VS.NET watch, but was "back" next time the user accessed the site.
I'm not sure what I'm doing wrong, but it's probably something simple.
I look forward to some help with this.

Ask a New Question

Become a Quomon Expert

Current Categories

 

Other Questions Needing Answers


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
Hi,

you use the Request.Cookies collection to check if a cookie is on the users machine. You create a Cookie adding it to the Response.Cookies collection. The Cookie handling is a bit strange in ASP.NET 1.x. It seems that if you access Request.Cookies, all Cookies from Response.Cookies get copied over to Request.Cookies. So checking for an existing cookie using Request.Cookies is ok. If you add a cookie, use Response.Cookie. If you want to access this cookie, use Request.Cookie and the new cookie gets copied over. Confusing? You bet!

You shouldn't (or bette you can't easily) delete a cookie. A cookie gets removed from the clients machine if it expires. Set the Expires proprty of the cookie to now:
Response.Cookies["TheCookie"].Expires = DateTime.Now;
The operating system should clean out the expired cookie.

Hope this helped

Peter

Expert:

PeterNZ

Date:

Jul 19, 2006

Time:

21:39

 

Votes: Good (0) | Bad (0)
Login to rate this answer

hey, dustPuppy...

Peter got most of the cookie stuff correct. Just an augmentation to what he put down for the expiration, I usually set the cookie to expire one day ago, just to make sure that it cant be used: Response.Cookies["myCookies"].Expires = DateTime.Now.AddDays(-1);

You can remove values in the cookie, as well.

Let's say you've got a cookie and one of the keys is "foo" with the value of "bar"

myCookie.Values.Add("foo", "bar");

Once you're read in the cookie (using Request.Cookies as Peter shows above) you can remove the value for "foo".
myCookie = Requst.Cookies["theCookie"];
myCookie.Values.Remove("foo");

now, you've got to add the cookie back into the Cookie collection:
Response.Cookies.Add(myCookie);

If you want to see what is contained in the cookie now you can use:
Response.Write(myCookiesValues.ToString());

Have fun...

Ric

Expert:

rcastagna

Date:

Jul 21, 2006

Time:

20:27

 

Votes: Good (0) | Bad (0)
Login to rate this answer

dustPuppy,

I "fat-fingered" the last statement in my post above...not enough coffee, I guess.

It should read:
Response.Write(myCookie.Values.ToString());

That'll work much better...

Ric

Expert:

rcastagna

Date:

Jul 24, 2006

Time:

03:59

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Thanks, Peter and Ric.
I'm surprised it's not more intuitive to do, but well now I've learned it :)

Expert:

dustPuppy

Date:

Jul 25, 2006

Time:

01:59

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Cookies get set and read via HTTP headers, and once you understand the flow, it gets much easier!

Setting and reading cookies is a dialog between the server and the client.

When setting a cookie, the server adds it to the response headers and when the browser reads the headers, it should set the cookie accordingly. Therefore, always use the Response collection to set them.

Next time the client requests a page on your domain, it will add the (unexpired) cookies for that domain to the request headers, which will then be read by your server script (the Request collection).

The reason why cookies reasonably could copied from Response to Request automatically is that otherwise the cookie would still show as unset, even though you have just set it, until the user hits the next page or refreshes the current. But actually it kind of cheating when the server does this :-)

Jakob

Expert:

jgivoni

Date:

Feb 18, 2007

Time:

12:14

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, and points have been rewarded to the following experts:

PeterNZ: 50
rcastagna: 25

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for this question by clicking on the "Answer Summaries" Tab.

 
No summaries have been submitted yet. Want to be the first?



Respond to this question:

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

 

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password
   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment