如何配置WCF仅对TimeStamp标头进行签名

时间:2011-05-09 10:43:31

标签: c# .net wcf ws-security ws-i

我正在尝试配置我的WCF客户端以创建包含WS-Addressing,WS-Security和TLS的SOAP 1.1请求。

安全要求是消息包含用户名令牌,TimeStamp以及使用包含的BinarySecurityToken对TimeStamp进行签名。

我使用了以下link中的示例来创建我的WCF客户端绑定。我稍微修改了示例(见下文),以便将HTTPS用作传输机制,MessageSecurity基于UsernameOverTransport。

            HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();            
        // the message security binding element will be configured to require 2 tokens:
        // 1) A username-password encrypted with the service token
        // 2) A client certificate used to sign the message

        // Instantiate a binding element that will require the username/password token in the message (encrypted with the server cert)
        TransportSecurityBindingElement messageSecurity = SecurityBindingElement.CreateUserNameOverTransportBindingElement();

        // Create supporting token parameters for the client X509 certificate.
        X509SecurityTokenParameters clientX509SupportingTokenParameters = new X509SecurityTokenParameters();
        // Specify that the supporting token is passed in message send by the client to the service
        clientX509SupportingTokenParameters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
        // Turn off derived keys
        clientX509SupportingTokenParameters.RequireDerivedKeys = false;
        // Augment the binding element to require the client's X509 certificate as an endorsing token in the message
        messageSecurity.EndpointSupportingTokenParameters.Endorsing.Add(clientX509SupportingTokenParameters);

        // Create a CustomBinding based on the constructed security binding element.
        return new CustomBinding(messageSecurity, httpsTransport);

此客户端生成的SOAP消息非常接近满足我正在调用的服务的要求,唯一的问题是正在签署wsa:To地址以及TimeStamp地址。

有没有办法准确指定哪些WCF标头已签名?因为我需要限制客户端只签署TimeStamp标头。

2 个答案:

答案 0 :(得分:0)

使用自定义邮件标题,您可以执行以下操作:

//... rest of MessageContract

[MessageHeader(ProtectionLevel = ProtectionLevel.Sign)]
string MyCustomHeader;

//... rest of MessageContract

但是我不相信这会影响您的情况,因为您尝试签署由自定义绑定插入的soap标头。要修改这些标头,您可能需要实现IClientMessageInspector interface并向客户端配置添加自定义行为以对TimeStamp标头进行签名。不确定如何访问证书以进行签名,但this may give you a good start.

答案 1 :(得分:0)

我知道这是一个老问题,但有几次我被问到这个问题。

我设法通过将messageVersion指定为Soap11而不是Soap11WSAddressing10来实现此目的,然后手动添加WS-Addresing标头,这避免了手动实现签名机制的需要。