4

If I add a cookie to the response via Response.Cookies.Add(), the cookie also appears in Request.Cookies.

Is there any way to get to the original request cookies (ignoring newly added response cookies) without somehow caching Request.Cookies in advance?

There is another question that asks why this situation exists (a point that I'm clear on). I'm asking if there's any way around it.

Update

As a very rough filter, it seems you can look for cookies in Request.Cookies whose Expires is equal to default(DateTime). This is because when browsers serialize their cookies in the request, they don't include their expiration dates.

However, this wouldn't get rid of any cookies accidentally added to Response without an Expires.

Community
  • 1
  • 1
Lobstrosity
  • 3,928
  • 29
  • 23

1 Answers1

2

Request.Headers["Cookie"] contains the raw header value sent by the browser. It's a semicolon-delimited list of key-value pairs. E.g.,:

key1=value1; key2=value2

Which can be parsed to a Dictionary<string, string> to figure out the request's original cookies (ignoring any that were added or modified by manipulating Request.Cookies or Response.Cookies).

(The values are URI-encoded to avoid any Bobby Tables situations for values containing equal signs or semicolons.)

Lobstrosity
  • 3,928
  • 29
  • 23