获得证书的名单在C#中的证书存储(Get list of certificates from the

2019-06-27 05:37发布

对于一个安全的应用程序,我需要选择在对话框的证书。 我如何才能获得证书存储区或它的一部分(例如storeLocation="Local Machine"storeName="My" )使用C#和从那里得到的所有证书的集合? 在此先感谢您的帮助。

Answer 1:

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 certificate in store.Certificates){
    //TODO's
}


Answer 2:

试试这个:

//using System.Security.Cryptography.X509Certificates;
public static X509Certificate2 selectCert(StoreName store, StoreLocation location, string windowTitle, string windowMsg)
{

    X509Certificate2 certSelected = null;
    X509Store x509Store = new X509Store(store, location);
    x509Store.Open(OpenFlags.ReadOnly);

    X509Certificate2Collection col = x509Store.Certificates;
    X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, windowTitle, windowMsg, X509SelectionFlag.SingleSelection);

    if (sel.Count > 0)
    {
        X509Certificate2Enumerator en = sel.GetEnumerator();
        en.MoveNext();
        certSelected = en.Current;
    }

    x509Store.Close();

    return certSelected;
}


Answer 3:

要做到这一点最简单的方法就是打开你想要的证书存储,然后使用X509Certificate2UI

var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var selectedCertificate = X509Certificate2UI.SelectFromCollection(
    store.Certificates, 
    "Title", 
    "MSG", 
    X509SelectionFlag.SingleSelection);

更多信息X509Certificate2UI MSDN上 。



Answer 4:

是- X509Store.Certificates属性返回的X.509证书存储的快照。



文章来源: Get list of certificates from the certificate store in C#