The certificate that was used has a trust chain that cannot be verified问题

时间:2021-08-11 22:09:49

今天调用wcf程序的时候发现证书有问题。报的错误如下 The certificate that was used has a trust chain that cannot be verified. Replace the certificate or change the certificateValidationMode. A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider 查找证书发现是自己的证书没有问题,服务器也是没有问题。 后来发现只要在客户端的代码添加一句话就好了。  ServiceReference1.TransportServiceClient proxy = new WCFTest2.ServiceReference1.TransportServiceClient();

proxy.ClientCredentials.UserName.UserName = "xxxx";

proxy.ClientCredentials.UserName.Password = "xxxx";

proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;  //重点

原因是我的证书放的位置不对,X509CertificateValidationMode有以下几种方式

None 未执行任何证书验证。

PeerTrust 如果证书位于被信任的人的存储区中,则有效。

ChainTrust 如果该链在受信任的根存储区生成证书颁发机构,则证书有效。

PeerOrChainTrust 如果证书位于被信任的人的存储区或该链在受信任的根存储区生成证书颁发机构,则证书有效。

Custom 用户必须插入自定义 X509CertificateValidator 以验证证书。

那这个配置主要是在服务器端的配置服务器上。

服务器的web.config配置

<clientCertificate >

<certificate findValue="XuanhunClient"

storeLocation="CurrentUser"

storeName="My"

x509FindType="FindBySubjectName"/>

<authentication certificateValidationMode="None" trustedStoreLocation="CurrentUser"  />

</clientCertificate>

服务器里面是用的none,那么客户端也需要用None.这里需要保持一致。

但针对https里面加证书了,因为这个证书是在iis的站点增加的。所以不需要增加。

就只要用

ServiceReference1.TransportServiceClient proxy = new WCFTest2.ServiceReference1.TransportServiceClient();

proxy.ClientCredentials.UserName.UserName = "xxxx";

proxy.ClientCredentials.UserName.Password = "xxxx";

Util.SetCertificatePolicy();

就好了。