我有以下代码来获取证书:
X509Store store = new X509Store("??","??");
List<X509Certificate2> lst = new List<X509Certificate2>();
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 mCert in store.Certificates)
{
lst.Add(mCert);
//TODO's
}
Now I want to get all the certificates installed on Local Machine in a list<> with Certificate Name, Their location, Issued with Public key Or Private Key(in Yes or No only) and the name of folder which contains those certs(please refer below snapshot):
用证书详细信息填充列表<>后,我想以网格格式显示这些数据。如何修改此代码以获得上述详细信息?
最佳答案
您机器上的证书存储在不同的商店中,因此您需要打开所有这些证书。请看 MSDN 文章。
代码示例:
public class CertDetails
{
public string Name { get; set; }
public string HasPrivateKey { get; set; }
public string Location { get; set; }
public string Issuer { get; set; }
}
// stores and they friendly names
var stores = new Dictionary<StoreName, string>()
{
{StoreName.My, "Personal"},
{StoreName.Root, "Trusted roots"},
{StoreName.TrustedPublisher, "Trusted publishers"}
// and so on
}.Select(s => new {store = new X509Store(s.Key, StoreLocation.LocalMachine), location = s.Value}).ToArray();
foreach (var store in stores)
store.store.Open(OpenFlags.ReadOnly); // open each store
var list = stores.SelectMany(s => s.store.Certificates.Cast<X509Certificate2>()
.Select(mCert => new CertDetails
{
HasPrivateKey = mCert.HasPrivateKey ? "Yes" : "No",
Name = mCert.FriendlyName,
Location = s.location,
Issuer = mCert.Issuer
})).ToList();
关于c# - 获取本地机器上安装的所有证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45116450/