首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF REST WebHttpBinding cookie处理

WCF REST WebHttpBinding cookie处理
EN

Stack Overflow用户
提问于 2011-03-22 23:54:31
回答 2查看 2.7K关注 0票数 0

问题是WCF客户端不尊重服务器cookie (不会在下一个请求中设置它)。下面是我创建客户端的方法:

代码语言:javascript
复制
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的方法。

我现在使用的自定义检查器,工作正常,但我正在寻找一个“干净”的解决方案:

代码语言:javascript
复制
  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;
            }
         }
      }
   }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-23 13:40:45

How to add cookie on a HttpTransportBindingElement查看此页面,它已完整地回答了您的问题

票数 0
EN

Stack Overflow用户

发布于 2013-05-10 02:55:13

我不知道这是不是在更新的.NET版本(我的版本是4.5)中被“修复”了,或者OP有没有其他问题导致它无法工作,但是allowCookies设置确实可以达到这个目的。

它对我很有效……我还在服务器上设置了"aspnetcompatabilitymode“。

代码语言:javascript
复制
  <webHttpBinding>
    <binding name="webHttpEndpointBinding" allowCookies="true">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"/>
      </security>
    </binding>
  </webHttpBinding>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5393994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档