Search code examples
c#restx509passphrase

Add Certificate X509 with passphrase key to Webservice request


I'm having trouble with adding a new SSL certificate to my webService request.

                var client = new RestClient(tokenUrl);
                string certif = String.Format("{0}/client.cer", CertifPath);
                string key = String.Format("{0}/client.key", CertifPath);

                if (File.Exists(certif) && File.Exists(key))
                {

                    X509Certificate2 cert = new X509Certificate2(certif, key); 
                    X509CertificateCollection collection1 = new X509CertificateCollection();
                    collection1.Add(cert);
                    client.ClientCertificates = collection1; 
                }

I'm getting as a response : 400 no required ssl certificate was sent nginx !!!!.

In Addition : When i use PostMan Or SoapUI .I a must add a third secret key(passphrase) to be able to get response. ex :Add certificate via postman

My Question is How can i add this third parameter(secret key) in my request c# ?.

There is another way to implement certificate to my request ???


Solution

  • Solution :

    I wasted a lot of time to search for how can include three informations (Certificat.cer,certif.key and the passphrase) in one Rest call. The solution was easy:

    1. the certificate Object is very flexible : i can encapsulate the three information in one certificate called (.pfx) using OpenSSL.
    2. you can install OpenSSL via : http://slproweb.com/download/Win64OpenSSL_Light-1_1_0g.exe
    3. After this commande : openssl pkcs12 -export -out certificate.pfx -inkey client.key -in client.cer.
    4. A new file is generated : certificate.pfx.
    5. So Now I can easly include my new certificate without any ERRORS :) .