Soumyajeet Nayak
Soumyajeet Nayak

Reputation: 37

Difference between Response.Cookies and Response.AddHeader "Set-Cookie"

In classic ASP, when I am setting a cookie using Response.Cookies("data1") = "value1" then I am able to read this cookie using Request.Cookies("data1") on the same page

But when I am using the syntax Response.AddHeader "Set-Cookie", "data2=value2" then I am not able to read this cookie using Request.Cookies("data2") on the same page.

So What is the difference between these two syntaxes of setting cookie and if I want to read the cookie using the second syntax how sould the read statement look like

Upvotes: 0

Views: 3319

Answers (1)

user692942
user692942

Reputation: 16672

Both methods set the HTTP header

set-cookie

but with a key difference.

Response.Cookies is a collection that is pre-built then when the response is ready to send, the HTTP header set-cookie is created. This means that for the life of the page where the Cookie collection is specified, the values are available to manipulate as much as you want.

Response.AddHeader() sets the HTTP header set-cookie when the response is sent back to the client, it has no association at all to Response.Cookies() and setting

Response.AddHeader("set-cookie", "...")

will not magically populate the Response.Cookies collection. The only way to populate the Cookies collection without using Response.Cookies() is to make a round trip to the server after Response.AddHeader() has been set.

Upvotes: 2

Related Questions