Get list of certificates from the certificate stor

2019-01-10 22:39发布

For a secure application I need to select a certificate in a dialog. How can I access certificate store or a part of it (e.g. storeLocation="Local Machine" and storeName="My") using C# and get a collection of all certificates from there? Thanks in advance for your help.

4条回答
SAY GOODBYE
2楼-- · 2019-01-10 23:13
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 certificate in store.Certificates){
    //TODO's
}
查看更多
够拽才男人
3楼-- · 2019-01-10 23:16

Try this:

//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;
}
查看更多
Deceive 欺骗
4楼-- · 2019-01-10 23:24

The simplest way to do that is by opening the certificate store you want and then using X509Certificate2UI.

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

More information in X509Certificate2UI on MSDN.

查看更多
祖国的老花朵
5楼-- · 2019-01-10 23:32

Yes -- the X509Store.Certificates property returns a snapshot of the X.509 certificate store.

查看更多
登录 后发表回答