0

I need to make request to a secure server. In order to authenticate in postman, I send over a .cer and .key + password

postman settings that work

But how can I add .key + password to a WebRequest in C#?

X509Certificate2 certificate = new X509Certificate2("~/Cert/mykey-wru.key", "PASSWORD");

...doenst work...

2 Answers2

1

My answer is combinde with creating the suggestes .pfx file and adding the cert to the local machine store.

than i needed to read them from there in order to get a seure ssl connection

foreach (X509Certificate2 certificate in store.Certificates)
            {
                if (certificate.FriendlyName.Contains("mycert"))
                    request.ClientCertificates.Add(certificate);
            }
0

You should use this constructor with .pfx certificate file.

create this file with openssl (you need .key and .crt files):

openssl pkcs12 -export -out cert.pfx -inkey cert.key -in cert.crt

this answer might be helpful for finding openssl

Victor E.
  • 21
  • 3
  • hmpf... i tried making and adding the .pfx with password and adding the .cre this works fine in postman agian, but not in c#, somethins is still missing there – FastSchonBacon Bacon Jun 29 '21 at 20:31
  • @FastSchonBaconBacon What exception you have? – Victor E. Jun 29 '21 at 20:37
  • "Cannot create SSL/TSL connection..." is what i am getting now – FastSchonBacon Bacon Jun 29 '21 at 21:28
  • @FastSchonBaconBacon, looks like there is a problem with trusting the server certificate. Can you really trust him? If yes, then we will TEMPORARILY ignore this problem: httpWebRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; But this is extremely unsafe. This is for testing purposes only. Be sure to remove this code afterwards. – Victor E. Jun 29 '21 at 21:46
  • @FastSchonBaconBacon, can you make the request in the browser? the postman has a setting to ignore certificate problems. And did I understand correctly that at the moment when reading the certificate, an exception does not occur now? – Victor E. Jun 29 '21 at 21:52
  • i've added the code but i still get the error "The request was aborted: Could not create SSL/TLS secure channel" – FastSchonBacon Bacon Jun 30 '21 at 19:15
  • ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; is also not working for me – FastSchonBacon Bacon Jun 30 '21 at 19:16