Search code examples
asp.netasp.net-coressl-certificatex509certificatex509certificate2

How to read X509 Signing Certificate content from string in .net?


I have X509 Signing Certificate inside of a string like:

var signingCertificate = -----BEGIN CERTIFICATE-----\r\nMIICTjCCAbegAw.........-----END CERTIFICATE-----

Now I want to read the content of this certificate. i know we can do it using X509Certificate2 object but that reads from file directly. Is there anyway to read the content from string?


Solution

  • You can convert your string to byte array, and create a X509Certificate2 object from it.

    byte[] bytes = Encoding.ASCII.GetBytes(signingCertificate);
    var x509Certificate2 = new X509Certificate2(bytes);