Wcf – C# WCF client error “The private key is not present in the X.509 certificate”

private-keywcfwcf-clientwcf-securityx509certificate

I'm am trying to create a very simple WCF client application which will send SOAP messages to a 3rd party service. All the messages must be digitally signed. It's really a proof of concept before I add the code to a larger application.

I have a .cer file containing the certificate and a .pem file containing the private key. What I have been trying to do is load the certificate using the .cer file then fire off a message. But I get the following error "The private key is not present in the X.509 certificate".

The problem, well one of the problems, is that I really know almost nothing about WCF, digital certificates, private keys and all that guff. I've done some reading, I've Googled till I'm blue in the face and I've not got anywhere.

If I open the .cert file there is a section labelled "BEGIN ENCRYPTED PRIVATE KEY" which suggests that the private key is included in the certificate. So, why am I getting a message saying that it is not present? Also, if the private key does need to be added to the certificate how do I do it?

Here basically what I'm doing. It's not my actual code, but it includes all the relevant stuff:

MyWSClient c = new MyWSClient();
c.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"pathToFile.cer");
c.SomeValidCall();

Best Answer

How are you creating this cert / key pair? You can combine them into a PKCS#12 certificate using openssl:

openssl pkcs12 -export -in yourcert.crt -inkey yourprivkey.key -out newcert.p12 

You may need to play with the input format to get it to work with a .pem private key.