Get list of certificates from the certificate store in C#

后端 未结 4 1505
情歌与酒
情歌与酒 2020-11-29 03:57

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\" an

4条回答
  •  暖寄归人
    2020-11-29 04:28

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

提交回复
热议问题