首页 > 解决方案 > 将证书添加到 WCF 客户端。找不到 X.509 证书

问题描述

我有一个 WCF 客户端,它将使用该服务颁发的证书对某些 Web 服务进行身份验证。起初,我的客户使用如下 https 绑定:

var httpsBinding = new BasicHttpsBinding();
httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;

但这给出了以下错误:

InvalidOperationException:未提供客户端证书。在 ClientCredentials 中指定客户端证书。

然后我将以下代码添加到我的客户端配置中:

this.ChannelFactory.Credentials.ClientCertificate.SetCertificate("test", System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, 
                System.Security.Cryptography.X509Certificates.StoreName.My); 

现在我得到了错误

System.InvalidOperationException:“使用以下搜索条件找不到 X.509 证书:StoreName 'My'、StoreLocation 'LocalMachine'、FindType 'FindBySubjectDistinguishedName'、FindValue 'test'。”

我绝对确定证书放在本地计算机上的个人文件夹中,但它仍然找不到它。我尝试将证书放在各种文件夹中,重命名它,使用指纹进行识别,但我的应用程序仍然找不到它。这里可能是什么问题?

标签: wcfsslcertificatex509certificate

解决方案


我建议您使用X509FindType.FindByThumbprint.

ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
//client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, "cbc81f77ed01a9784a12483030ccd497f01be71c");
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "9ee8be61d875bd6e1108c98b590386d0a489a9ca");

它对应于以下值。 为了让 WCF 服务可以访问这个本地证书,我们通常将帐户添加到证书私钥的管理组中。 此外,WCF 服务使用证书对客户端进行身份验证,这通常需要我们在客户端同时设置服务证书和客户端证书。 如果有什么我可以帮忙的,请随时告诉我。
在此处输入图像描述
Everyone


推荐阅读