WCF消息凭据实现详细信息

时间:2009-10-28 13:36:02

标签: c# .net wcf wcf-binding credentials

我正在寻找一些技术细节,说明使用WCF绑定在消息交换期间存储实际用户名+密码(凭据)的位置。

<bindings>
    <wsHttpBinding>
        <binding name="wsHttp">
            <security mode="TransportWithMessageCredential">
                <transport/>
                <message clientCredentialType="UserName" 
                         negotiateServiceCredential="false" 
                         establishSecurityContext="true" />
            </security>
        </binding>
    </wsHttpBinding>
</bindings>

然后在客户端应用程序中,我调用此服务传递一组有效的信用证,如此

using (SupplierServiceClient client = new SupplierServiceClient()) {
            client.ClientCredentials.UserName.UserName = "admin";
            client.ClientCredentials.UserName.Password = "password";

            SupplierList = client.GetSupplierCollection();
}

起初我假设WCF正在获取这些数据并将其放入SOAP标头但是它从WSDL中看起来没那么...任何帮助?

修改

以下是客户端的安全配置在生产中的样子

<security mode="TransportWithMessageCredential">
    <transport clientCredentialType="None" />
    <message clientCredentialType="UserName" establishSecurityContext="false" />
</security>

2 个答案:

答案 0 :(得分:3)

通过设置UserNameCredentials,您实际上正在利用用户名令牌配置文件。这会导致令牌作为SOAP标头添加到消息中。 SOAP标头看起来像这样:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
        xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <env:Header>
        <wsse:UsernameToken>
            <wsse:Username>jdoe</wsse:Username>
            <wsse:Password>passw0rd</wsse:Password>
            <wsse:Nonce><!-- optional nonce here --></wsse:Nonce>
        </wsse:UsernameToken>
    </env:Header>
    <env:Body>
        <!-- body here -->
    </env:Body>
</env:Envelope>

现在,我不确定你为什么要提到WSDL。尽管WSDL应该在操作中包含正确的WS-Policy声明,但令牌不会出现在WSDL中。这将让WSDL的消费者发现他们实际上需要将UsernamePasswordToken发送给请求。

最后,有人提出了RequestSecurityToken(RST),但是如果您只是使用简单的UsernameToken身份验证,则不应该(需要)是一个RST。 RST需要参与的唯一时间是使用WS-Trust与安全令牌服务器(STS)进行令牌交换。

答案 1 :(得分:0)

以下是SOAP消息示例的样子:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/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:01d3a7d2-dc5a-42cf-acf0-3dd6bd50230e</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <a:To s:mustUnderstand="1">http://localhost:9999/Service1.svc</a:To>
  </s:Header>
  <s:Body>
    <t:RequestSecurityToken Context="uuid-7c240c06-c14b-42af-82dd-10f44f423928-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">YGsGBisGAQUFAqBhMF+gJDAiBgorBgEEAYI3AgIKBgkqhkiC9xIBAgIGCSqGSIb3EgECAqI3BDVOVExNU1NQAAEAAAC3shjiBgAGAC8AAAAHAAcAKAAAAAUBKAoAAAAPU0cyMjA1Nk1BVE1VVA==</t:BinaryExchange>
    </t:RequestSecurityToken>
  </s:Body>
</s:Envelope>
相关问题