CA Certificate broken in C# but works in Python
I have a CA certificate as Base64 strings as Certificate itself and RSA Key. Both files are working perfectly in my python app and Insomnia.
But in C# I keep getting wierd errors:
I load it with that:
var cert3 = X509Certificate2.CreateFromPem(certificateInformation.OAuth.Certificate); using RSA privateKey = RSA.Create(); privateKey.ImportFromPem(certificateInformation.OAuth.Key.ToCharArray()); X509Certificate2 certWithPrivateKey = cert3.CopyWithPrivateKey(privateKey); var _pfxCertificate = new X509Certificate2(certWithPrivateKey.Export(X509ContentType.Pfx));
Then adding it to HttpClient:
var handler = new HttpClientHandler(); handler.ClientCertificates.Add(_pfxCertificate); var client = new HttpClient(handler);
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest() { ClientId = certificateInformation.OAuth.ClientId, GrantType = "client_credentials", Address = certificateInformation.OAuth.TokenUrl, Method = HttpMethod.Post, });
I tried around 20 to 30 different approaches, e.g. saving the cert as files and loading them - disable the ServerCertificateCustomValidationCallback but I cant get any further.
In Windows I got the error "Ephermate certifidats not supported" on which I started to use " var _pfxCertificate = new X509Certificate2(certWithPrivateKey.Export(X509ContentType.Pfx)); ", then I get a "The certificate chain was issued by an authority that is not trusted.". In Linux (Dockerize my app) I get "error:0A000418:SSL routines::tlsv1 alert unknown ca"
Again, if a simple insomnia call with exactly the same cert would not work, I would easily think my cert is broken...