KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a certificate installed on my machine and when I go to view it, I see the message "You have a private key that corresponds to this certificate" however, when I try to access that private key in code, it is null. I use the following code to get my certificate: var x509Certificate = GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=SomeCert"); Where: public X509Certificate2 GetCertificate(string storeName, string storeLocation, string subjectName) { var store = new X509Store(getStoreName(storeName), getStoreLocation(storeLocation)); X509Certificate2Collection certificates = null; store.Open(OpenFlags.ReadOnly); try { X509Certificate2 result = null; certificates = store.Certificates; return getCertificateResult(certificates, subjectName, result); } finally { if (certificates != null) { foreach (var cert in certificates) { cert.Reset(); } } store.Close(); } } And: private static X509Certificate2 getCertificateResult(IEnumerable certificates, string subjectName, X509Certificate2 result) { foreach (var cert in certificates.Cast<X509Certificate2>().Where(cert => cert.SubjectName.Name != null && cert.SubjectName.Name.ToLower() == subjectName.ToLower())) { if (result != null) { throw new ApplicationException(string.Format("There is more than one certificate found for subject Name {0}", subjectName)); } result = new X509Certificate2(cert); } if (result == null) { throw new ApplicationException(string.Format("No certificate was found for subject Name {0}", subjectName)); } return result; } I get my certificate back fine, however when I try to access the private ke
Tags (comma-separated)
Save Edits
Cancel