获取安装在本地机器上的所有证书 [英] Get all certificates installed on Local machine
问题描述
我有以下代码来获取证书:
X509Store store = new X509Store("??","??");列表lst = new List();store.Open(OpenFlags.ReadOnly);foreach (X509Certificate2 mCert in store.Certificates){lst.Add(mCert);//待办事项}
<块引用>
现在我想在 Local Machine 中安装所有证书一个列表<>,带有证书名称,他们的位置,用公钥颁发或私钥(仅是或否)和文件夹的名称包含这些证书(请参阅下面的快照):
使用证书详细信息填充 List<> 后,我想以网格格式显示这些数据.如何修改此代码以获取上述详细信息?
您机器上的证书存储在不同的存储区中,因此您需要将它们全部打开.请参阅 MSDN文章.
代码示例:
公共类 CertDetails{公共字符串名称{获取;放;}公共字符串 HasPrivateKey { 获取;放;}公共字符串位置{获取;放;}公共字符串发行人{获取;放;}}//商店和他们友好的名字var store = new Dictionary(){{StoreName.My, "个人"},{StoreName.Root, "受信任的根"},{StoreName.TrustedPublisher, "可信发布商"}//等等}.Select(s => new {store = new X509Store(s.Key, StoreLocation.LocalMachine), location = s.Value}).ToArray();foreach (var store in store)store.store.Open(OpenFlags.ReadOnly);//打开每个商店var list = stores.SelectMany(s => s.store.Certificates.Cast().Select(mCert => new CertDetails{HasPrivateKey = mCert.HasPrivateKey ?是":否",名称 = mCert.FriendlyName,位置 = s.location,发行人 = mCert.Issuer})).ToList();
I have the following code to get the certificates:
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):
After populating List<> with Certs details I want to display those data in a grid format. How to modify this code to get above details?
Certificates on your machine stored in a different stores, so you need open all of them. Please see that MSDN article.
Code example:
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();
这篇关于获取安装在本地机器上的所有证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!