WCF下SOAP Web服务同时进行客户端证书与用户名认证的配置问题
问题描述
我在对接某要求同时完成客户端证书和用户名认证的SOAP Web服务时遇到了认证失败的问题。
目标服务共支持三种认证模式:
- 用户名+密码:我使用
BasicHttpsSecurityMode.Transport可正常完成该模式的认证 - 仅证书认证:因多种原因我无法使用该模式
- 证书+用户名:注意该场景下密码留空,我始终无法完成该模式的认证
所有认证场景均基于TLS 1.2版本的HTTPS协议。
我不清楚如何在WCF中配置绑定和认证参数以适配第三种认证模式,我已经尝试了多种BasicHttpsBinding配置和CustomBinding方案。
在下方代码中,当endpointConfig.Username和endpointConfig.ClientCertificate都被指定时,我参考搜索到的示例构建了CustomBinding,但运行失败。客户端确实向服务端发送了正确的证书,服务端返回Change Cipher Spec报文后,客户端直接终止了TLS连接。
目前我得到的报错信息如下:
Could not establish trust relationship for the SSL/TLS secure channel with authority 'ws1c.czebox.cz'.
Could not establish secure channel for SSL/TLS with authority 'ws1c.czebox.cz'.
我用于构建服务客户端的原始代码如下:
// prepare binding Binding binding; // NOTE A username alone is sufficient for Basic authentication e.g. when logging-in to a DataBox using the HostCert method if (!String.IsNullOrEmpty(endpointConfig.Username) && endpointConfig.ClientCertificate != null) { // client certificate and username + password // the below code is experimental and just doesn't work (yields the first error mentioned above) var tb = new HttpsTransportBindingElement(); tb.MaxReceivedMessageSize = MaxSupportedMessageSize; tb.RequireClientCertificate = true; tb.AuthenticationScheme = AuthenticationSchemes.Basic; var ub = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); binding = new CustomBinding(ub, tb); } else if (!String.IsNullOrEmpty(endpointConfig.Username)) { // username + password var b = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport); b.MaxReceivedMessageSize = MaxSupportedMessageSize; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; binding = b; } else if (endpointConfig.ClientCertificate != null) { // client certificate var b = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport); b.MaxReceivedMessageSize = MaxSupportedMessageSize; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; binding = b; } else { throw new NotSupportedException(); } // prepare endpoint identity EndpointIdentity identity = null; X509Certificate2 cert = null; if (endpointConfig.ClientCertificate != null) { var clientCredentials = new ClientCredentials(); clientCredentials.ClientCertificate.SetCertificate( endpointConfig.ClientCertificate.StoreLocation, endpointConfig.ClientCertificate.StoreName, endpointConfig.ClientCertificate.FindBy, endpointConfig.ClientCertificate.FindValue); cert = clientCredentials.ClientCertificate.Certificate; identity = EndpointIdentity.CreateX509CertificateIdentity(cert); } // prepare endpoint address var address = new EndpointAddress(new Uri(endpointConfig.EndpointUrl), identity); // create connector var connector = new dmInfoPortTypeClient(binding, address); // setup username + password authentication // NOTE Password may be empty when logging-in to a DataBox using the HostCert method if (!String.IsNullOrEmpty(endpointConfig.Username)) { connector.ClientCredentials.UserName.UserName = endpointConfig.Username; connector.ClientCredentials.UserName.Password = endpointConfig.Password; } // setup client-certificate authentication if (endpointConfig.ClientCertificate != null) { connector.ClientCredentials.ClientCertificate.Certificate = cert; }
注意:完全没有服务端的控制权,该服务是政务服务,无法协商调整服务端配置。运行环境为.NET 4.7.x,可以升级到.NET 4.8,但无法迁移到.NET Core。
解决方案
1. 修正CustomBinding配置
原有双认证分支多添加了消息层的用户名安全绑定元素,导致重复封装请求、客户端协商后主动断开连接。修改代码如下:
if (!String.IsNullOrEmpty(endpointConfig.Username) && endpointConfig.ClientCertificate != null) { var tb = new HttpsTransportBindingElement(); tb.MaxReceivedMessageSize = MaxSupportedMessageSize; tb.RequireClientCertificate = true; tb.AuthenticationScheme = AuthenticationSchemes.Basic; // 移除SecurityBindingElement,仅保留传输层配置,同时处理客户端证书+Basic认证 binding = new CustomBinding(tb); }
2. 显式配置TLS版本和服务端证书校验
报错的核心原因之一是服务端证书未被本地信任,添加如下代码(初始化客户端前执行即可):
// 显式指定TLS 1.2版本,避免系统默认配置不匹配 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // 服务端证书校验,生产环境请替换为政务服务官方提供的服务端证书指纹做精准校验 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => { // 示例:校验证书指纹,此处替换为官方提供的大写指纹值 return cert.GetCertHashString().Equals("XXXXX官方提供的证书指纹XXXXX", StringComparison.OrdinalIgnoreCase); };
3. 移除错误的EndpointIdentity配置
原有代码向EndpointAddress传入了客户端证书的身份标识,属于参数传错,直接去掉identity参数即可:
var address = new EndpointAddress(new Uri(endpointConfig.EndpointUrl));
修改完成后即可正常适配证书+用户名的双认证场景:TLS层完成客户端证书认证,HTTP头携带Basic Auth的用户名信息(密码为空),完全匹配政务服务的要求。
内容的提问来源于stack exchange,提问作者Ondrej Tucny
相关产品推荐
相关产品推荐

