【问题标题】:Unable to Instantiate X509Certificate2 from Byte Array无法从字节数组实例化 X509Certificate2
【发布时间】:2017-03-15 07:29:54
【问题描述】:

我正在尝试使用自签名证书来配置 IdentityServer3, 我使用 openssl 创建了一个 x509 证书,如下所示: openssl req -x509 -sha256 -days 365 -key key.pem -in csr.csr -out certificate.pem 然后使用以下方法合并密钥和证书: pkcs12 -export -in my-cert.pem inkey my-key.pem -out xyz-cert.pfx 然后我将 xyz-cert.pfx 的内容转换为存储在 web.config 中的密钥中的 Base64String,然后尝试使用证书实例化 X509Certificate2,如下所示:

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
        };

然后抛出以下异常:

我不知道我哪里弄错了。 感谢您的帮助

【问题讨论】:

    标签: c# .net cryptography asp.net-web-api2 x509certificate2


    【解决方案1】:
    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
    };
    

    【讨论】:

    • 谢谢,这不起作用,在尝试了这个之后它不起作用,我去创建一个新的密钥,测试,它的工作。不知道我在创建初始密钥时出了什么问题
    • 那么它最终是使用这个还是使用您的原始代码?
    猜你喜欢
    • 2022-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多