C# How to get all certificates from "My" Store


public static List<string> GetCertNamesFromStore(string search)
        {
            X509Store store = new X509Store("My");
            try
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = store.Certificates;
                X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
                var res = new List<string>();
                foreach (X509Certificate2 s in certCollection)
                {
                    if (search=="")
                        res.Add(GetSubjectCN(s));
                    else if (s.SubjectName.Name.Contains(search))
                        res.Add(GetSubjectCN(s));
                }
                return res;
            }
            finally
            {
                store.Close();
            }
        }

No comments:

Post a Comment