0
votes

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

1
That is because Response.AddHeader adds the headers during the response (data back to the client) whereas Response.Cookies pre builds the collection before the response is sent, then sets set-cookie HTTP header when sending the response, this means until the response is sent there is time to modify the cookie collection. - user692942

1 Answers

2
votes

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.