检查Windows密钥库中是否安装了最终用户证书?

时间:2011-06-23 09:03:42

标签: c# certificate keystore

如果在用户窗口密钥库(个人)中安装了PKI最终用户证书,有没有办法检查C#? (会有例外吗?)我会传递一些属性,比如Name。

1 个答案:

答案 0 :(得分:8)

您可以使用X509Store类在系统上搜索证书。下面的代码示例在当前用户的个人存储中找到按主题名称“XYZ”的证书。

System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly); // Dont forget. otherwise u will get an exception.
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName,"XYZ",true);
if(certs.Count > 0)
{
    // Certificate is found.
}
else
{
    // No Certificate found by that subject name.
}