Reputation: 4077
I'm working through the .NET API for working with X509 certificates and would like to know if it is possible to retrieve a CA Certificate from CertificateAuthority Store. I have tried loads of permutations and the one that makes the most sense (but also fails) is:
var store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var count = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=SecureTrust CA", false).Count;
store.Close();
That returns a count of 0
.
Upvotes: 0
Views: 428
Reputation: 239664
As indicated in the comments, you need to use Root
rather than CertificateAuthority
. Also, if you're using FindBySubjectDistinguishedName
, you have to use the full DN.
This works:
var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var count = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName,
"CN=SecureTrust CA, O=SecureTrust Corporation, C=US", false).Count;
store.Close();
Or use the less strict FindBySubjectName
:
var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var count = store.Certificates.Find(X509FindType.FindBySubjectName,
"SecureTrust CA", false).Count;
store.Close();
Upvotes: 1