WCF客户端SOAP消息缺失wsse:Security安全头问题排查
WCF客户端添加wsse:Security安全头及消息加密解决方案
问题根源
当前配置中basicHttpBinding的安全模式设置为Transport,仅启用了SSL传输层加密,不会生成wsse:Security消息头。要实现SOAP消息的加密和签名,需要启用消息层安全或传输+消息混合安全。
解决步骤
1. 修改绑定配置
更新basicHttpBinding的安全设置,将模式改为TransportWithMessageCredential(传输层SSL+消息层加密),并配置消息安全的客户端凭证类型:
<bindings> <basicHttpBinding> <binding name="MessageAndCertificateClient"> <!-- 启用传输+消息混合安全 --> <security mode="TransportWithMessageCredential"> <!-- Transport层保持SSL证书验证 --> <transport clientCredentialType="Certificate" /> <!-- Message层使用证书进行消息加密/签名 --> <message clientCredentialType="Certificate" /> </security> </binding> </basicHttpBinding> </bindings>
2. 调整客户端代码配置
确保服务证书用于消息加密,客户端证书用于身份验证和签名:
var wcfClient = new WcfClient(); // certificate1用于SSL传输层客户端身份验证 wcfClient.ClientCredentials.ClientCertificate.Certificate = GetCertificate("certificate1"); // certificate2用于SOAP消息加密(服务公钥证书) wcfClient.ClientCredentials.ServiceCertificate.DefaultCertificate = GetCertificate("certificate2"); var secAuth = new X509ServiceCertificateAuthentication(); secAuth.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; secAuth.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck; // 配置服务证书验证规则 wcfClient.ClientCredentials.ServiceCertificate.Authentication = secAuth; var response = wcfClient.CheckData(model).GetAwaiter().GetResult();
3. 移除冗余的服务端配置
当前配置中的<services>和<serviceBehaviors>属于服务端配置,客户端无需保留,可删除以避免混淆:
<!-- 客户端配置中删除以下内容 --> <behaviors> <serviceBehaviors> <behavior name="ServiceCredentialsBehavior"> <serviceCredentials> <serviceCertificate findValue="xxxx" x509FindType="FindBySubjectName" /> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="ServiceCredentialsBehavior" name="Test"> <endpoint address="xxxxx" binding="basicHttpBinding" bindingConfiguration="MessageAndCertificateClient" name="SecuredByClientCertificate" contract="Test" /> </service> </services>
验证结果
修改后,WCF客户端会自动生成包含wsse:Security头的SOAP消息,对消息体进行加密,并添加客户端证书的安全令牌和签名信息,与预期格式一致。
内容的提问来源于stack exchange,提问作者pakerinjo
相关产品推荐
相关产品推荐

