I have written my own CertificateManager. I would like to store the LocalServiceProviderCertificate in the Windows Certificate Store. However, I am having trouble retrieving the certificate. When I call Find on the X509Store object, it returns a collection with zero items. What is the correct method to get a X509Certificate2 object with both keys from the Windows Certificate Store so that I can provide it to the LocalServiceProviderCertificate property?
Here is the code I am using:
LocalServiceProviderCertificate = FindCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "www.idp.com");
private static X509Certificate2 FindCertificate(StoreLocation location, StoreName name, X509FindType findType, string findValue) { X509Store store = new X509Store(name, location); try { // create and open store for read-only access store.Open(OpenFlags.ReadOnly); // search store X509Certificate2Collection col = store.Certificates.Find(findType, findValue, true); // return first certificate found return col[0]; } // always close the store finally { store.Close(); } }
|