【问题标题】:Add Certificate X509 with passphrase key to Webservice request将带有密码短语密钥的证书 X509 添加到 Web 服务请求
【发布时间】:2018-08-24 16:46:40
【问题描述】:

我在向我的 webService 请求添加新 SSL 证书时遇到问题。

                var client = new RestClient(tokenUrl);
                string certif = String.Format("{0}/client.cer", CertifPath);
                string key = String.Format("{0}/client.key", CertifPath);

                if (File.Exists(certif) && File.Exists(key))
                {

                    X509Certificate2 cert = new X509Certificate2(certif, key); 
                    X509CertificateCollection collection1 = new X509CertificateCollection();
                    collection1.Add(cert);
                    client.ClientCertificates = collection1; 
                }

我收到了回复:400 no required ssl certificate was sent nginx !!!!.

另外:当我使用 PostMan 或 SoapUI 时。我必须添加第三个密钥(密码)才能获得响应。 ex :Add certificate via postman

我的问题是如何在我的请求 c# 中添加第三个参数(密钥)?。

还有另一种方法可以根据我的请求实施证书???

【问题讨论】:

    标签: c# rest x509 passphrase


    【解决方案1】:

    你能用那段简洁的代码让你做到这一点吗:

    byte[] certBuffer = Helpers.GetBytesFromPEM(publicCert, PemStringType.Certificate);
    byte[] keyBuffer  = Helpers.GetBytesFromPEM(privateKey, PemStringType.RsaPrivateKey);
    
    X509Certificate2 certificate = new X509Certificate2(certBuffer, password);
    
    RSACryptoServiceProvider prov = Crypto.DecodeRsaPrivateKey(keyBuffer);
    certificate.PrivateKey = prov;
    

    【讨论】:

    • 认为 Zakariya 为您的评论 :) 解决方案非常简单!!! :我应该将所有三个信息(.cer、.key 和密码)封装在一个证书(.pfx)中。
    【解决方案2】:

    解决方案:

    我浪费了很多时间来搜索如何在一个 Rest 调用中包含三个信息(Certificat.cer、certif.key 和密码)。 解决方案很简单:

    1. 证书对象非常灵活:我可以封装 使用 OpenSSL 的一个名为 (.pfx) 的证书中包含三个信息。
    2. 您可以通过以下方式安装 OpenSSL:http://slproweb.com/download/Win64OpenSSL_Light-1_1_0g.exe
    3. 在此命令之后: openssl pkcs12 -export -out certificate.pfx -inkey client.key -in client.cer.
    4. 生成一个新文件:certificate.pfx。
    5. 所以现在我可以轻松地包含我的新证书而不会出现任何错误:)。

    【讨论】:

      猜你喜欢
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2016-01-06
      相关资源
      最近更新 更多