Search code examples
c#authenticationhttpwebrequestssl-certificatex509certificate2

X509Certificate2 prompts me for password


I have exported my private key to a pfx file and I am using it to post some data to a server. When the code hits this line

X509Certificate2 cert = new X509Certificate2("C:\\mycerts\\dml.pfx","Passw0rd");

A prompt comes up and ask me to define a password. I write a new password 2 times. Then when I reach this line

Stream newStream = request.GetRequestStream();

It prompts me again and I put in the newly defined password and the post is successfull.

Is there a way I can avoid user interaction?


Solution

  • So I found out my certificate had a wicked Crypto service provider. Using certutil -dump I would get this

    ---------------- End Nesting Level 1 ----------------

    Provider = CRYPTOMATHiC RSA Full Provider 1.2

    Simple container name: Verify

    Unique container name: Verify

    ERROR: missing key association property: CERT_KEY_IDENTIFIER_PROP_ID

    Cannot load key: Key does not exist. 0x8009000d (-2146893811 NTE_NO_KEY)

    Encryption test FAILED

    Dont know exactly why this is bad but I managed to run a fixing tool and after that it would display

    ---------------- End Nesting Level 1 ----------------

    Provider = Microsoft Enhanced RSA and AES Cryptographic Provider

    Encryption test passed

    Now it works without prompting for password