获取本地机器上安装的所有证书
Get all certificates installed on Local machine
我有以下代码来获取证书:
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):
在使用证书详细信息填充列表<> 后,我想以网格格式显示这些数据。如何修改此代码以获得上述详细信息?
一个简短的例子来激发你的灵感,也许会有点帮助:
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
...
X509Store store = null;
store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly)
...
//RSA CryptoServiceProvider
RSACryptoServiceProvider rsaCSP = null;
string keyPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Microsoft\Crypto\RSA\MachineKeys\";
string friendlyName = "";
foreach (X509Certificate2 mCert in store.Certificates) {
rsaCSP = mCert.PrivateKey as RSACryptoServiceProvider;
if (rsaCSP != null) {
friendlyName = mCert.FriendlyName;
keyPath += rsaCSP.CspKeyContainerInfo.UniqueKeyContainerName;
}
}
你机器上的证书存放在不同的商店,所以你需要打开所有的证书。请看 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();
我有以下代码来获取证书:
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):
在使用证书详细信息填充列表<> 后,我想以网格格式显示这些数据。如何修改此代码以获得上述详细信息?
一个简短的例子来激发你的灵感,也许会有点帮助:
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
...
X509Store store = null;
store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly)
...
//RSA CryptoServiceProvider
RSACryptoServiceProvider rsaCSP = null;
string keyPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Microsoft\Crypto\RSA\MachineKeys\";
string friendlyName = "";
foreach (X509Certificate2 mCert in store.Certificates) {
rsaCSP = mCert.PrivateKey as RSACryptoServiceProvider;
if (rsaCSP != null) {
friendlyName = mCert.FriendlyName;
keyPath += rsaCSP.CspKeyContainerInfo.UniqueKeyContainerName;
}
}
你机器上的证书存放在不同的商店,所以你需要打开所有的证书。请看 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();