【问题标题】:The private key is not present in the X.509 certificate from code bu available in certificate manager证书管理器中提供的代码 bu 中的 X.509 证书中不存在私钥
【发布时间】:2021-02-16 02:18:24
【问题描述】:

所以我尝试使用证书在我的解决方案和基于 SOAP 的服务之间建立通信。

我已将他们的证书和我的证书安装到证书存储中。

我已确保我的证书有一个与之对应的私钥: Certificate has private key corresponding to it

但是,当从商店将我的证书加载到我的代码中时,我收到错误消息“X.509 证书中不存在私钥。”

我打印了以下内容,可以看到 Private Key 为空且 ContainsPrivateKey == False ClientCertificatePublicKey: System.Security.Cryptography.X509Certificates.PublicKey, ClientCertificatePrivateKey: , ContainsPrivateKey False

到目前为止我已经尝试过:

将keystorageflags设置为:

 X509KeyStorageFlags.MachineKeySet|
    X509KeyStorageFlags.PersistKeySet|
    X509KeyStorageFlags.Exportable

在应用程序池中将加载用户配置文件设置为 true

以下是我如何从商店读取证书的 sn-p

private byte[] ReadCertificate(string certificateThumbprint)
        {
            X509Store store = new X509Store("MY",StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

            X509Certificate2Collection collection = 
            (X509Certificate2Collection)store.Certificates;
            X509Certificate2Collection signingCert = 
            collection.Find(X509FindType.FindByThumbprint, 
            certificateThumbprint,false);

            byte[] rawdata = signingCert[0].RawData;
            store.Close();
            return rawdata;
        }

有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: c# x509certificate private-key


    【解决方案1】:
    byte[] rawdata = signingCert[0].RawData;
    

    实际上,这仅返回证书的公共部分,没有私钥引用。相反,您应该考虑返回整个 X509Certificate2 对象。

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2012-10-25
      相关资源
      最近更新 更多