How to add multiple cookies to Response in WCF / REST service

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

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.OutgoingResponse.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

回答1:

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` 


回答2:

You could use the HttpContext.Current.Response.SetCookie instead.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!