使用BasicHttpBinding和Windows身份验证使用WCF服务

时间:2011-11-18 14:57:09

标签: .net wcf

我有一个带有以下绑定的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);

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

  

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

我理解这个错误经常是因为使用SOAP 1.1和wsHttpBinding的basicHttpBinding使用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;
}

1 个答案:

答案 0 :(得分:1)

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

事实证明,我试图解决错误“客户端和服务绑定可能不匹配。”,这使我使客户端和服务器端点绑定TOO类似。关键是在客户端端点上明确指定TextEncoding为UTF-8,并在客户端上使用wsHttpBinding,即使我连接到服务器上的basicHttpBinding。这使得消息在SOAP 1.2中始终从始发地到目的地,而无需在服务器上设置SSL安全性。

感谢您帮助找到合适的解决方案!

Kris C,希望这对你下一步的工作有用:)