Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 07:53 - Jul 19, 2006  

dustPuppy

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.

Categories

Answer Discussion
Tutorials

 

PeterNZ

Date:: Jul 19, 2006

Time:: 21:39

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

rcastagna

Date:: Jul 21, 2006

Time:: 20:27

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

rcastagna

Date:: Jul 24, 2006

Time:: 03:59

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

dustPuppy

Date:: Jul 25, 2006

Time:: 01:59

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

jgivoni

Date:: Feb 18, 2007

Time:: 12:14

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

Question Answered

This question has been closed, 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 a Tutorial in the Tutorial Area.

Answer 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:

Forgot Your Password?

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

Answer 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:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




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



Language Options

English:

www.quomon.com

Español:

www.quomon.es