使用WCF将WS-Security凭据添加到SOAP标头

时间:2011-10-10 23:53:15

标签: wcf soap ws-security soap-client wsse

我正在尝试与我无法控制的Java Web服务进行通信,并且我正在尝试创建一个可以正常工作的绑定。

  1. 标头中不允许使用时间戳,因此为了使用includeTimestamp="false"属性,我必须使用<customBinding>
  2. 他们正在使用MTOM,因此我必须使用<mtomMessagingEncoding>元素。
  3. 这是我的<bindings>元素:

    <bindings>
      <customBinding >
        <binding name="MyBindingName" >
          <mtomMessageEncoding  />
          <transactionFlow />
          <security authenticationMode="UserNameOverTransport"
                    includeTimestamp="false">            
          </security>
        </binding>
      </customBinding>
    </bindings>
    

    SOAP Web服务要求邮件头采用以下格式:

     <soap:Envelope ... >
      <soap:Header ... >
        <wsse:UsernameToken>
          <wsse:Username>doo</wsse:Username>
          <wsse:Password Type="wsse:PasswordText">fuss</wsse:Password>
        </...>
      </...>
     </...>
    

    我最接近的是:

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" 
                xmlns:a="http://www.w3.org/2005/08/addressing" 
                xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <s:Header>
        <a:Action s:mustUnderstand="1"></a:Action>
        <a:MessageID>urn:uuid:a368e205-a14d-4955-bf75-049cdd3a78c0</a:MessageID>
        <a:ReplyTo>
          <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand="1">https://blablabla</a:To>
        <o:Security s:mustUnderstand="1" 
                    xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <o:UsernameToken u:Id="uuid-0f1e399b-31a8-4e00-a57f-277c21e94879-1">
          <o:Username><!-- Removed--></o:Username>
          <o:Password><!-- Removed--></o:Password>
        </o:UsernameToken>
       </o:Security>
     </s:Header>
    

    我确信我在这里遗漏了一些琐碎和愚蠢的东西,但对于我的生活,我无法弄清楚它可能是什么。

1 个答案:

答案 0 :(得分:5)

您还必须配置消息版本,因为默认情况下它使用WS-Addressing:

<bindings>
  <customBinding >
    <binding name="MyBindingName" >
      <mtomMessageEncoding messageVersion="Soap11" /> <!-- or Soap12 -->
      <security authenticationMode="UserNameOverTransport"
                includeTimestamp="false">            
      </security>
    </binding>
  </customBinding>
</bindings>

TransactionFlow元素根本不需要。

顺便说一下。您显示的消息不是WS-Security令牌的有效使用,因为它必须位于Security元素内,因此如果它确实是Java服务所期望的,那么它不符合WS-Security规范,您将不得不使用自定义消息改为标题。