通过SSL使用纯文本用户名凭据为第三方SOAP 1.1服务配置WCF客户端

时间:2012-08-19 13:16:20

标签: wcf soap wcf-security ws-security basichttpbinding

我正在尝试连接到需要SSL安全性和用户名/密码凭据的第三方SOAP 1.1服务。预期的一个例子是:

<soapenv:Header>
    <wsse:Security>
        <wsse:UsernameToken>
            <wsse:Username>username</wsse:Username>
            <wsse:Password>password</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>

我的客户端配置如下:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="thirdpartyservicebindingconfig">
                <security mode="TransportWithMessageCredential">
                    <message clientCredentialType="UserName"
                             algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://..." 
                  binding="basicHttpBinding"
                  bindingConfiguration="thirdpartyservicebindingconfig"
                  contract="thirdpartyservicecontract" 
                  name="thirdpartyserviceendpoint" />
    </client>
</system.serviceModel>

服务客户端代码是:

var client = new thirdpartyservicecontractclient();

client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";

var result = client.DoSomething();

我收到以下错误异常消息:

  

安全处理器无法在中找到安全标头   信息。这可能是因为消息是不安全的故障或   因为沟通之间存在绑定不匹配   派对。如果为安全性配置了服务,则会发生这种情况   客户端没有使用安全性..

修改
如果我将安全模式重新配置为“运输”:
<security mode="TransportWithMessageCredential">
我从第三方服务收到错误:

  

com.sun.xml.wss.XWSSecurityException:消息不符合   已配置的策略[AuthenticationTokenPolicy(S)]:无安全性   标题发现;嵌套异常是   com.sun.xml.wss.XWSSecurityException:   com.sun.xml.wss.XWSSecurityException:消息不符合   已配置的策略[AuthenticationTokenPolicy(S)]:无安全性   找到了标题。

如何配置客户端以连接此服务?

  • 使用SSL上的纯文本密码的WS安全

1 个答案:

答案 0 :(得分:5)

Rick Strahl也遇到了同样的问题。 Here's the link在他的博客文章中描述和解决问题。

问题:

  

问题是WCF期望响应中有一个TimeStamp Soap头。   如果你看一下你会看到的出站响应和Soap标题   那里有一个时间戳。时间戳预计是   返回Soap响应返回。请注意,这不是一个   WS-Security的要求所以WCF在这里做了一些“特殊”的事情   这实际上打破了这个服务电话。

解决方案:

BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
elements.Find<SecurityBindingElement>().IncludeTimestamp = false;
client.Endpoint.Binding = new CustomBinding(elements);
  

上面的代码显式修改了Binding配置   从出站调用中删除时间戳,删除   要求服务器返回它。这使得WCF很开心   电话会通过。

相关问题