C# 从证书存储中获取证书列表

发布于 2024-07-29 07:20:28 字数 145 浏览 7 评论 0原文

对于安全的应用程序,我需要在对话框中选择一个证书。 如何使用 C# 访问证书存储或其一部分(例如 storeLocation="Local Machine"storeName="My")并从中获取所有证书的集合那里?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

任性一次 2024-08-05 07:20:28
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 certificate in store.Certificates){
    //TODO's
}
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 certificate in store.Certificates){
    //TODO's
}
海风掠过北极光 2024-08-05 07:20:28

尝试这个:

//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;
}

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;
}
茶色山野 2024-08-05 07:20:28

最简单的方法是打开所需的证书存储,然后使用 X509Certificate2UI

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

更多信息请参见 < MSDN 上的代码>X509Certificate2UI

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.

如若梦似彩虹 2024-08-05 07:20:28

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

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

铃予 2024-08-05 07:20:28

以上述问题为例。

    public List<string> getListofCertificate()
    {
        var certificates = new List<string>();
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly);

            // Place all certificates in an X509Certificate2Collection object.
            X509Certificate2Collection certCollection = store.Certificates;
            foreach (X509Certificate2 x509 in certCollection)
                {
                    Console.WriteLine(x509.IssuerName.Name);
                    certificates.Add(x509.IssuerName.Name);
                }
        }
        finally
        {
            store.Close();
        }
        return certificates;

    }

Example for the above question.

    public List<string> getListofCertificate()
    {
        var certificates = new List<string>();
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly);

            // Place all certificates in an X509Certificate2Collection object.
            X509Certificate2Collection certCollection = store.Certificates;
            foreach (X509Certificate2 x509 in certCollection)
                {
                    Console.WriteLine(x509.IssuerName.Name);
                    certificates.Add(x509.IssuerName.Name);
                }
        }
        finally
        {
            store.Close();
        }
        return certificates;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文