nichifor
nichifor

Reputation: 11

The private key is not present in the X.509 certificate from code bu available in certificate manager

So I am trying to use certificates to establish communication between my solution and a SOAP based service.

I have their certificate and my certificate installed into the certification store.

I have made sure that my certificate has a private key that corresponds to it: Certificate has private key corresponding to it

However when loading my certificate in my code from the store I get the error "The private key is not present in the X.509 certificate."

I printed the following and can see that the Private Key is empty and ContainsPrivateKey == False ClientCertificatePublicKey: System.Security.Cryptography.X509Certificates.PublicKey, ClientCertificatePrivateKey: , ContainsPrivateKey False

So far I have tried:

Setting the keystorageflags to:

 X509KeyStorageFlags.MachineKeySet|
    X509KeyStorageFlags.PersistKeySet|
    X509KeyStorageFlags.Exportable

Setting Load User Profile to true in Applicaion Pool

Below is a snippet of how I read the certificate from the store

private byte[] ReadCertificate(string certificateThumbprint)
        {
            X509Store store = new X509Store("MY",StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

            X509Certificate2Collection collection = 
            (X509Certificate2Collection)store.Certificates;
            X509Certificate2Collection signingCert = 
            collection.Find(X509FindType.FindByThumbprint, 
            certificateThumbprint,false);

            byte[] rawdata = signingCert[0].RawData;
            store.Close();
            return rawdata;
        }

Does anybody have any idea on how I can fix this?

Upvotes: 1

Views: 625

Answers (1)

Crypt32
Crypt32

Reputation: 13974

byte[] rawdata = signingCert[0].RawData;

actually, this returns only public part of the certificate, without private key reference. Instead, you shall consider to return entire X509Certificate2 object.

Upvotes: 1

Related Questions