Search code examples
c#.netcryptographyasp.net-web-api2x509certificate2

Unable to Instantiate X509Certificate2 from Byte Array


I am trying to use a self-signed certificate to configure IdentityServer3, I used openssl to create a x509 certificate as follows: openssl req -x509 -sha256 -days 365 -key key.pem -in csr.csr -out certificate.pem then merged the key and cert using : pkcs12 -export -in my-cert.pem inkey my-key.pem -out xyz-cert.pfx i then converted the content of xyz-cert.pfx to Base64String which is stored in a key in web.config, then tried to use the certificate to instantiate X509Certificate2 as follows:

var certificate = Convert.FromBase64String(ConfigurationManager.AppSettings["SigningCertificate"]);

        var options = new IdentityServerOptions
        {
            SigningCertificate = new X509Certificate2(certificate, ConfigurationManager.AppSettings["SigningCertificatePassword"]),
            RequireSsl = false, // DO NOT DO THIS IN 
            Factory = factory
        };

the following exception is then thrown: enter image description here

I can't figure out where i got it wrong. Thanks for your help


Solution

  • var options = new IdentityServerOptions
    {
        string CertText = ConfigurationManager.AppSettings["SigningCertificatePassword"];
        byte[] certBytes = Convert.FromBase64String(certText);
        SigningCertificate = new X509Certificate2(certificate, certBytes),
        RequireSsl = false, // DO NOT DO THIS IN 
        Factory = factory
    };