【发布时间】:2011-08-25 19:49:57
【问题描述】:
我正在尝试以编程方式使用证书,而不是使用商店。我正在使用文件名和密码创建X509Certificate2。
当我手动将根证书添加到受信任的人的证书存储中时,这可以正常工作。但是,我宁愿不必在每次部署时都这样做——我也宁愿以编程方式处理它。
当我从证书存储中删除根证书时,我得到了一个例外。
我读到的所有内容似乎都在说我必须手动将根证书添加到证书存储中,否则信任链将无法工作。
问题:有没有一种程序化的方式来设置信任链,所以我不必手动进行?
代码如下:
var serverCert = new X509Certificate2("FullPathToMyCertificate.cer", "Password");
Client.ClientCredentials.ServiceCertificate.DefaultCertificate = serverCert;
当我尝试使用客户端时发生的异常是:
System.IdentityModel.Tokens.SecurityTokenValidationException
The X.509 certificate CN=notrealcertname, OU=TPA, OU=BMP, OU=Projects, O=Somebody, C=US is not in the trusted people store.
The X.509 certificate CN=notrealcertname, OU=TPA, OU=BMP, OU=Projects, O=Somebody, C=US chain building failed.
The certificate that was used has a trust chain that cannot be verified.
Replace the certificate or change the certificateValidationMode.
A certificate chain could not be built to a trusted root authority.
【问题讨论】:
标签: c# certificate x509