首页 > 解决方案 > SOAP WCF 将 Signature 和 BinarySecurityToken 添加到标头

问题描述

我需要更多东西,签名必须在 Header 标记内的 Security 标记内,并带有 BinarySecurityToken 元素

就像:

<soapenv:Header> <!-- extrac of the example file -->
  <wsse:Security soapenv:mustUnderstand="1" 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-wss-wssecurity-utility-1.0.xsd">
    <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-D53CCD6983E4CE0BD7142791021078262">
        MIIDbDgg4iF74cqiF6NcnzBnD9qA2MB6hSo38e0RISilEFSzWikDqBtOjgm7ux9fdeHojDm4uvhsSfbEyGmGTAQRzg9yIiD3ovjOzuZsf+I3HWS9F6xl6sb2+wvYXD4DFk/OD+N7UszGsoWFZg
    </wsse:BinarySecurityToken>
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
          .....
    </ds:Signature>

我可以直接从客户端证书以编程方式填充 xml 元素,如下所示:

var cert = new X509Certificate2(ClientCertificateFilePath, ClientCertificatePassword);
        var export = cert.Export(X509ContentType.Cert, ClientCertificatePassword);
        var base64 = Convert.ToBase64String(export);

问题是如何将 Signature 和 BinarySecurityToken 添加到标头?

标签: c#xmlwcfsoapwsse

解决方案


手动编写 SOAP 信封来调用服务可能是一种选择。
以下是有关如何添加 SOAP 标头的一些讨论。
什么是 C# WCF AddressHeader 的 Java Apache CXF 等价物?
这通常是因为服务器端的Web服务不是WCF,因此我们可能无法通过WCF调用服务。
在我看来,上述 SOAP 信封格式决定了 WCF 使用的绑定的通道形状,这可能与使用证书对客户端进行身份验证的 WCF 服务兼容。

BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

当服务器使用证书对客户端进行身份验证时,客户端使用相同的绑定类型并向服务器提供证书,并在 SOAP 信封中附加证书的签名。下面是 Fiddler 捕获的与服务器端通信时的 Http 流量。他们的肥皂信封是相似的。 有关使用证书对客户端进行身份验证的更多信息,请参阅以下链接。https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/message-security-with-a-certificate-client https://docs.microsoft.com/en-us/dotnet /framework/wcf/feature-details/transport-security-with-certificate-authentication 如果有什么我可以帮忙的,请随时告诉我。
在此处输入图像描述




推荐阅读