【问题标题】:Adding wsse:security element to soap header将 wsse:security 元素添加到肥皂标题
【发布时间】:2012-04-25 20:02:41
【问题描述】:

我目前正在尝试连接到 hmrc 的 api,并在我的项目中设置了服务,现在正在创建要发送的消息。

我已经能够很好地创建邮件的正文部分,但是在正确设置标题时遇到了麻烦。这是消息应该是什么样子的示例:

<soap:Envelope  
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing"  
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"  
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd"  
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Header>     
<wsa:Action>http://schemas.xmlsoap.org/ws/2004/04/security/trust/RST/Issue</wsa:Action> 
<wsa:MessageID>uuid:39610e5b-162b-4491-9301-85578046a583</wsa:MessageID> 
<wsa:ReplyTo>
    <wsa:Address>
        http://schemas.xmlsoap.org/ws/2004/03/addressing/role/anonymous
    </wsa:Address> 
</wsa:ReplyTo> 
<wsa:To>
    http://www.gateway.gov.uk/soap/2007/02/sts
</wsa:To> 
<wsse:Security> 
  <wsu:Timestamp wsu:Id="Timestamp-201ebb7a-d2f4-44d5-85e4-b8bb4c71ed54"> 
    <wsu:Created>2012-04-13T16:18:11Z</wsu:Created> 
    <wsu:Expires>2012-04-13T16:23:11Z</wsu:Expires> 
  </wsu:Timestamp> 
</wsse:Security> 
</soap:Header> 
<soap:Body> 
<wst:RequestSecurityToken xmlns:wst="http://schemas.xmlsoap.org/ws/2004/04/trust"> 
<wst:TokenType>urn:GSO-SystemServices:external:1.65:gatewaytoken#GatewayToken</wst:TokenType>     
<wst:RequestType>http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue</wst:RequestType> 
  <wst:Base> 
    <wsse:UsernameToken xmlns:wsu="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-94d12e0f-400b-4412-bde8-e03357eb0692"> 
      <wsse:Username>513FDQ37JILL</wsse:Username> 
      <wsse:Password Type="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"> 
        NXJ3ynAa8Sy136aifThXJl8vLoE= 
      </wsse:Password> 
      <wsse:Nonce>4nJy2oD6xC79zatTHvuywg==</wsse:Nonce> 
      <wsu:Created>2007-02-01T11:03:10Z</wsu:Created> 
    </wsse:UsernameToken> 
  </wst:Base> 
  <wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy"> 
    <wsa:EndpointReference> 
      <wsa:Address>urn:GSO-SystemServices:external:1.65:GatewayWebServices</wsa:Address> 
    </wsa:EndpointReference> 
  </wsp:AppliesTo> Version 1.0 32 9 June 2008 
  <wst:Lifetime> 
    <wsu:Expires>2012-04-13T20:23:11Z</wsu:Expires> 
  </wst:Lifetime> 
</wst:RequestSecurityToken> 
</soap:Body> 
</soap:Envelope> 

主要问题是我无法显示安全元素。我目前一直在 app.config 文件中尝试各种类型的绑定,但我对使用服务真的很陌生,并且完全被卡住了。这是我当前的 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <system.serviceModel>
    <bindings>
        <customBinding>
            <binding name="XmlTokenServiceBinding" >
                <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                    messageVersion="Soap11WSAddressing10" writeEncoding="utf-8">
                 <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </textMessageEncoding>
                <security authenticationMode="SecureConversation" includeTimestamp="True" requireSecurityContextCancellation="false"
                        messageSecurityVersion="WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10">
               </security>                
               <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
                    maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                    bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                    keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                    realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                    useDefaultWebProxy="true" requireClientCertificate="false" />
            </binding>
        </customBinding>
    </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior>
        <clientVia viaUri="https://secure.gateway.gov.uk/soap/2007/02/sts/GatewayTokenService.ashx"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
    <client>
        <endpoint address="https://secure.gateway.gov.uk/soap/2007/02/sts/GatewayTokenService.ashx"
            binding="customBinding" bindingConfiguration="XmlTokenServiceBinding"
            contract="ServiceReference1.XmlTokenServicePortType" name="XmlTokenServicePort" />
    </client>
</system.serviceModel>

编辑:在 cmets 中请求调用服务的代码:

  TokenService.RequestSecurityToken request = CreateTokenRequest();   
  TokenService.XmlTokenServicePortTypeClient client = new TokenService.XmlTokenServicePortTypeClient();
  TokenService.RequestSecurityTokenResponseType response = client.RequestSecurityTokenRequest(request);

这里也是通过提琴手的请求消息:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
    <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
    <a:MessageID>urn:uuid:0f43afa7-c22c-400b-87b2-c9b76bd0abdd</a:MessageID>
    <a:ReplyTo>
        <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
        <a:To s:mustUnderstand="1">https://secure.gateway.gov.uk/soap/2007/02/sts/GatewayTokenService.ashx</a:To>
</s:Header>
<s:Body>
    <t:RequestSecurityToken Context="uuid-829d42ad-a560-44aa-9be8-95c212d82762-1" xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
        <t:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</t:TokenType>
        <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
        <t:KeySize>256</t:KeySize>
        <t:BinaryExchange ValueType="http://schemas.xmlsoap.org/ws/2005/02/trust/spnego" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">YIGeBgYrBgEFBQKggZMwgZCgGjAYBgorBgEEAYI3AgIeBgorBgEEAYI3AgIKonIEcE5FR09FWFRTAAAAAAAAAABgAAAAcAAAABOP7thI+QdbUguCiBDWyeqYiLGof7AdxPVl4cuUPGbTiMyHDzMox/EGGxas/mzB/AAAAAAAAAAAYAAAAAEAAAAAAAAAAAAAAEVyfDIyRYtIv9kqa6BepAo=</t:BinaryExchange>
    </t:RequestSecurityToken>
</s:Body>
</s:Envelope>

【问题讨论】:

  • 请显示您用来调用服务的代码。您不必“创建正文部分”或“正确获取标题”。 WCF 应该为你做这件事。
  • 您的问题让我相信您正在尝试手动构建 XML。您是否在操作 XML?
  • 好吧,正文部分需要一个 userToken,我必须创建并放入正文部分。至于标题,其余部分是为我创建的,只是这一节没有。
  • 再次,您所说的方式表明您正在构建 XML。您是在构建 XML,还是让序列化为您工作?

标签: c# wcf api soap wcf-binding


【解决方案1】:

您有任何额外的服务安全配置规范吗?似乎它不仅需要一条消息,还需要多条消息交换(ws-trust)。此外,示例消息使用 wcf 不直接支持的 ws-addressing 版本 2004/03。在您的情况下,所有这些可能会或可能不会成为问题(也许您只是直接与 STS 合作)

回答您的直接问题 - 您的设置应该发出一些安全标头。为什么你认为它没有? WCF 日志将不会显示安全性(除非配置为在传输级别记录)。使用 fiddler 来查看线路上的实际情况。

【讨论】:

  • 我已经在使用 fiddler 查看请求和响应,这就是我知道当前没有创建安全标头的方式。
  • 好的。请发布完整的工作肥皂样本(不仅仅是安全部分)和您发送的完整肥皂消息(通过 Fiddler)。
  • 好的,我已将完整示例和消息添加到我的原始帖子中
  • 我还是不明白一件事。您使用安全会话 (ws-secureconversation)。这需要多次往返服务器以创建令牌,然后真正的调用使用 toekn。通常,用户让 WCF 为他们进行协商,并且只进行真正的调用。您似乎想自己做所有象征性的事情。我说的对吗?
  • 嗯,我认为问题的一半是该服务没有创建消息预期包含的所有内容。所以我觉得我必须填补这些缺失的空白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多