Akaash Chintamani Meher

Akaash Chintamani Meher

  • 1.4k
  • 320
  • 15.1k

Authenticate the KEY File in Certificate using C#

Jun 28 2023 10:39 AM

As in Postman we have certificate and key file, how do I validate the KEY file using c# or MVC. I have tried adding the below code but seems like for the KEY file we've some other code to write. Please suggest some help.

string base64cert = Convert.ToBase64String(File.ReadAllBytes(pemFilePath));
var cert = new X509Certificate2(Convert.FromBase64String(base64cert), certAuthCode);
cert.PrivateKey = null;
handler.ClientCertificates.Add(cert);
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(new X509Certificate2(crtFilePath));
//For Key File Path
handler.ClientCertificates.Add(new X509Certificate2(crtKeyPath));

Answers (3)