WCF 4.0仅Cookie首先由浏览器记录 [英] WCF 4.0 Cookie Only First is Recorded by Browser

查看:212
本文介绍了WCF 4.0仅Cookie首先由浏览器记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WebHttpBinding从WCF自带主机控制台应用程序REST服务中以最快的方式编写Cookie。
我准备设置Cookie,但只有第一个cookie在浏览器中设置(使用Windows上的FireFox 11.0)以及Expires值。

I am working on the fastest way to write cookies from a WCF self hosted console app REST service with WebHttpBinding. I prepare the "Set Cookies" but only the first cookie is set in the browser (using FireFox 11.0 on Windows) as well as the "Expires" value.

var expiry = DateTime.Now.AddMonths( 6 );
var outresp = WebOperationContext.Current.OutgoingResponse;
outresp.Headers.Add( HttpResponseHeader.SetCookie , "cookie1=fd9416ea-2705-4d44-be76-f2f7b95e6b13; cookie2=7d489e69-d9c9-4b8d-a4b1-bce0da1c5966;expires=" + expiry );

我也尝试在第一个cookie和第二个cookie之间加入逗号,

I also tried putting a comma between the first cookie and the second one, that did not work either:

outresp.Headers.Add( HttpResponseHeader.SetCookie , "cookie1=fd9416ea-2705-4d44-be76-f2f7b95e6b13;, cookie2=7d489e69-d9c9-4b8d-a4b1-bce0da1c5966;expires=" + expiry );

我剪下这个,然后开启Fiddler:

I cut this down, and fired up Fiddler:

outresp.Headers.Add( HttpResponseHeader.SetCookie , "foo=foovalue;,bar=barvalue;" );

,Fiddler显示:

and Fiddler shows this:

Response sent 27 bytes of Cookie data:
    Set-Cookie: foo=foovalue;,bar=barvalue;

This response did not contain a P3P Header.

Validate P3P Policies at: http://www.w3.org/P3P/validator.html
Learn more at: http://www.fiddler2.com/redir/?id=p3pinfo

这告诉我我不是在.NET端格式化的东西,到浏览器。

Which tells me that I am not formatting things on the .NET side to make the cookies acceptable to the browser.

如何获取所有的cookies到浏览器中只有第一个值?我错过了什么?

How can I get all of the cookies into the browser insetad of just the first value? What did I miss?

感谢。

我将此问题提交给Microsoft以获得支持,并了解到:

I took the issue to Microsoft for support, and learned this:

Set-Cookie被嵌入单个Cookie行的事实作为Microsoft .NET Framework 4.0和Framework 4.5中的错误。
在Microsoft WCF开发组中,错误被列为已关闭和不会修复。

根据Microsoft,唯一的解决方法是从自托管转移到托管在IIS中,然后直接使用(IIS)Response对象(不包含错误的不同代码路径)。

The fact that "Set-Cookie" is munged into a single cookie line is logged as a bug within Microsoft for .NET Framework 4.0 and also Framework 4.5. Within the Microsoft WCF development group, the bug is listed as "closed" and "won't fix".
According to Microsoft, the only workaround is to move from self-hosted to hosting within IIS and then using the (IIS) Response object directly (different code path that does not have the bug).

推荐答案

这是一个较旧的帖子,因为这仍然是一个持续的问题,使用AspNetCompatibilityMode与WCF有显着的性能下降,MSFT提供的选项在许多/大多数/任何情况下是不可行的。我将要回答这个问题,因为这个问题仍然存在。

Although this is an older post, since this is still an ongoing issue and using the AspNetCompatibilityMode with WCF has significant performance downside, the option that MSFT has supplied is not viable under many/most/any circumstances. I'm going to answer this question late since this issue is still lingering.

解决这个问题的唯一方法是输出Set-Cookie响应头,就像你但是在客户端使用javascript来处理响应头,并将其放在浏览器中,因为,正如你所说的,浏览器不会处理这个响应。

The only way to resolve this issue is to output the Set-Cookie response header just as you've shown, but to process that response header on the client side with javascript and place it in to the browser since, as you've noted, the browser will not handle this response. Neither will applying more than one Set-Cookie response header work as noted in this bug I created.

http://connect.microsoft.com/ VisualStudio / feedback / details / 779111 / wcf-rest-service-two-set-cookie-http-headers-invalid-set-cookie-header-syntax

因此,处理响应头,并在客户端javascript中使用document.cookie将cookie放在浏览器的jQuery .ajax请求的成功处理程序中。

So, process the response header and use document.cookie in client-side javascript to place the cookies in the browser within the success handler of your jQuery .ajax request.

这篇关于WCF 4.0仅Cookie首先由浏览器记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆