【问题标题】:Consuming WCF Service with BasicHttpBinding and Windows Authentication使用 BasicHttpBinding 和 Windows 身份验证使用 WCF 服务
【发布时间】:2011-11-18 14:57:09
【问题描述】:

我有一个具有以下绑定的 WCF 服务:

<basicHttpBinding>        
  <binding name="bind" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
     <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message algorithmSuite="Default" clientCredentialType="UserName"/>
     </security>
  </binding>
</basicHttpBinding>

我正在使用此端点在代码中动态使用它:

BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "bind";
binding.MaxBufferSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
ServiceEndpoint endPoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ImyContract)), binding, new EndpointAddress(endPointAddress));

endPoint.Name = name;
ChannelFactory<ImyContract> channelFactory = new ChannelFactory<ImyContract>(endPoint);

当我浏览到该服务时,一切都按预期工作,但是当我通过应用程序连接时:

内容类型文本/xml; charset=utf-8 不受服务支持 http://localhost/SynchronizationService/SyncService.svc/DataBinding。 客户端和服务绑定可能不匹配。

我知道这个错误通常是因为 basicHttpBinding 使用 SOAP 1.1 而 wsHttpBinding 使用 1.2,但是,我没有在服务器上使用 SSL,并且有另一个使用流的端点,所以切换不是我的选择。我做错了什么导致我无法正确使用服务?

更新:

另外,我没有在客户端使用默认的 basicHttpBinding,而是设置安全性以匹配服务器的预期。我正在使用这种方法设置安全性:

private static BasicHttpSecurity SetEndpointSecurity()
{
    BasicHttpSecurity security = new BasicHttpSecurity();
    HttpTransportSecurity transport = new HttpTransportSecurity();
    BasicHttpMessageSecurity message = new BasicHttpMessageSecurity();

    security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

    transport.ClientCredentialType = HttpClientCredentialType.Windows;
    transport.ProxyCredentialType = HttpProxyCredentialType.None;
    transport.Realm = "";

    message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
    message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;

    security.Transport = transport;
    security.Message = message;

    return security;
}

【问题讨论】:

  • 尝试使用 svcutil 创建一个代理,看看它是如何在其中完成的。哦,您是否也在使用元数据交换端点。因为如果您不小心使用了该 uri,这通常也会导致此问题。
  • 删除了我的答案,因为我认为更多地查看 BasicHTTPBinding 是不正确的(有助于了解我接下来要尝试的事情)。以下链接(和 cmets)似乎是一个有用的起点:rickgaribay.net/archive/2007/04/04/…
  • 服务器版本确定使用上面指定的绑定吗?使用 .NET4,我发现在我的命名空间等正确之前,它提供的默认绑定 (wsHTTPBinding) 仍然有效并且我的配置未激活。使用@the_ajp 评论中描述的 svcutil 可能有助于检查。

标签: .net wcf


【解决方案1】:

感谢 the_ajp 建议让 .NET 构建代理类,然后在代码中重新实现其端点,因为这让我找到了正确的解决方案!

事实证明,我尝试解决错误“客户端和服务绑定可能不匹配。”导致我使客户端和服务器端点绑定过于相似。关键是在客户端端点上将 TextEncoding 显式指定为 UTF-8,并在客户端上使用 wsHttpBinding,即使我正在连接到服务器上的 basicHttpBinding。这样可以将 SOAP 1.2 中的消息从源头一直保存到目的地,而无需在服务器上设置 SSL 安全性。

感谢您帮助我们找到正确的解决方案!

Kris C,希望这对你接下来的工作有帮助:)!

【讨论】:

  • 谢谢 - 我在传输安全性(通道的 SSL/HTTPS)和 TransportCredentialOnly 之间弄错了,它通过未加密的凭据(因此实际上不安全)
  • 出于兴趣 - 对于您客户端上的 wsHTTPBinding,设置了什么安全模式以允许它在没有 SSL 的情况下工作?
  • @KrisC,在客户端设置为TransportCredentialOnly,传输层为Windows客户端凭据类型,消息层为UserName。
猜你喜欢
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 2012-10-24
相关资源
最近更新 更多