How to add multiple cookies to Response in WCF / REST service
I have access to WebOperationContext
and can add one cookie by doing this:
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a");
However if I call that several times, e.g.:
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a");
WebOperationContext.Current.Out开发者_Go百科goingResponse.Headers.Add("Set-Cookie: foo_b=bar_b");
I should get the following in my header (2 cookies):
Set-Cookie: foo_a=bar_a
Set-Cookie: foo_b=bar_b
But instead get:
Set-Cookie: foo_a=bar_a, foo_b=bar_b
How do I set multiple cookies? Thx
Turns out the cookies can be set on one Set-Cookie: header line, but you will need to place a ';' semicolon at the end of the cookie...
WebOperationContext.Current.OutgoingResponse.Headers.Add("Set-Cookie: foo_a=bar_a;,"
+ foo_b=bar_b;");
then the result will be: (which browser consider to be 2 cookies not just 1)
Set-Cookie: foo_a=bar_a;, foo_b=bar_b;
// browser sees this as 2 cookies: `foo_a` & `foo_b`
as opposed to
Set-Cookie: foo_a=bar_a, foo_b=bar_b
// browser sees this as 1 cookie: `foo_a` with value: `=bar_a, foo_b=bar_b`
You could use the HttpContext.Current.Response.SetCookie
instead.
精彩评论