问题是WCF客户端不尊重服务器cookie (不会在下一个请求中设置它)。下面是我创建客户端的方法:
WebChannelFactory<IPostService> factory = new WebChannelFactory<IPostService>(
new WebHttpBinding() {AllowCookies = true},
new Uri("http://10.6.90.45:8081"));
_proxy = factory.CreateChannel();将AllowCookies设置为false也不起作用。
我现在所做的是编写一个简单的IClientMessageInspector来持久化请求之间的服务器cookie,但我真的不想这样做,应该有一种以标准方式处理cookie的方法。
我现在使用的自定义检查器,工作正常,但我正在寻找一个“干净”的解决方案:
public class CookieInspector : IClientMessageInspector
{
private string _cookie;
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
if(_cookie != null && request.Properties.ContainsKey("httpRequest"))
{
HttpRequestMessageProperty httpRequest = request.Properties["httpRequest"] as HttpRequestMessageProperty;
if(httpRequest != null)
{
httpRequest.Headers["Cookie"] = _cookie;
}
}
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.Properties.ContainsKey("httpResponse"))
{
HttpResponseMessageProperty httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
if(httpResponse != null)
{
string cookie = httpResponse.Headers.Get("Set-Cookie");
if (cookie != null) _cookie = cookie;
}
}
}
}发布于 2011-03-23 13:40:45
How to add cookie on a HttpTransportBindingElement查看此页面,它已完整地回答了您的问题
发布于 2013-05-10 02:55:13
我不知道这是不是在更新的.NET版本(我的版本是4.5)中被“修复”了,或者OP有没有其他问题导致它无法工作,但是allowCookies设置确实可以达到这个目的。
它对我很有效……我还在服务器上设置了"aspnetcompatabilitymode“。
<webHttpBinding>
<binding name="webHttpEndpointBinding" allowCookies="true">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</webHttpBinding>https://stackoverflow.com/questions/5393994
复制相似问题