C#RestSharp客户端证书获得异常“无法建立SSL连接”

问题描述 投票:0回答:1

我在放置此代码的C#.Net Core 3.1和.Net Standard库中进行编程。

我没有使用未安装在服务器/计算机上的客户端证书的RestSharp来获得我的http请求,我将无法访问稍后将在其上运行的服务器/计算机。

我的代码:

                // Create a RestSharp RestClient objhect with the base URL
                var client = new RestClient(_baseAPIUrl);

                // Create a request object with the path to the payment requests
                var request = new RestRequest("swish-cpcapi/api/v1/paymentrequests");

                // Create up a client certificate collection and import the certificate to it
                X509Certificate2Collection clientCertificates = new X509Certificate2Collection();
                clientCertificates.Import(_certDataBytes, _certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

                // Add client certificate collection to the RestClient
                client.ClientCertificates = clientCertificates;

                //X509Certificate2 certificates = new X509Certificate2(_certDataBytes, _certificatePassword);

                //client.ClientCertificates = new X509CertificateCollection() { certificates };

                // Add payment request data
                request.AddJsonBody(requestData);

                var response = client.Post(request);
                var content = response.Content;

我做了我在互联网上能找到的一切,并且已经使用了具有自己生成的证书的服务测试环境,并在那里的生产环境中生成了具有相同结果的自己的证书。

我已经测试过设置TLS 1.1或TLS 1.2进行测试,他们在自己的curl示例中指定了TLS 1.1。

有人知道吗?

更新2020-01-08-我从服务技术支持那里得到的信息是,他们只看到我发送一个证书,但是当我尝试调试并检查X509Certificate2Collection时,我发现了3个证书。但是他们说只看到一个?

c# .net .net-core restsharp client-certificates
1个回答
1
投票

恕我直言,如果您尝试访问的服务通过带有期望的公钥/私钥的SSL证书进行保护,那么如果没有该证书,则无法访问它。如果那是要保护服务的意图,那么我认为您只能检查服务运行状况检查,或者最多检查服务是否可访问(没有客户端证书)

但是就像HTTPS一样,如果您可以从浏览器访问服务URL,例如访问其元数据或简单的GET或其他内容,则可以尝试从浏览器下载证书。在Chrome浏览器中,您可能会在URL之前看到一个锁定符号(安全图标)。单击该按钮,则可以下载公共证书。您可以将其安装在计算机上并尝试。

[如果该服务具有发布证书,并且访问不需要客户端证书,则上述选项可能有效。我希望您正在安装CERT并通过有权访问证书的帐户通过代码访问该证书。假设您将证书安装在LocalSystem下,那么可能需要管理员从代码/解决方案访问才能访问该证书路径。或将证书安装在current_user路径下。

作为第一步,我将检查是否能够从浏览器访问该服务。

这是我从您的问题中所了解的。同样,如果您可以基于公用密钥/专用密钥解释该服务是否是受保护的服务,则需要具有访问/证书才能使用该服务。

UPDATE:通过创建演示api和演示客户端以使用客户端证书,我尝试了几种选择。根据我对您查询的了解,我认为以下可能是您可以尝试的一些选择。

// --- using the cert path if you have the path (instead of the byte[])
var myCert = new X509Certificate2("<path to the client cert.cer/.pfx>", "secure-password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
X509CertificateCollection clientCerts = new X509CertificateCollection();
clientCerts.Add(myCert);

OR

var certStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine); //replace with appropriate values based on your cert configuration
certStore.Open(OpenFlags.ReadOnly);

//option 1 (ideally a client must have the cert thumbprint instead of a password 
var cert = certStore.Certificates.Find(X509FindType.FindByThumbprint, "<cert Thumbprint>", false);

//option 2 (explore other options based on X509NameTypes
var cert = certStore.Certificates.OfType<X509Certificate2>()
                    .FirstOrDefault(cert => cert.GetNameInfo(X509NameType.DnsName, false) == "mycompany.dns.name.given in the cert");

client.ClientCertificates = new X509CertificateCollection(cert);
© www.soinside.com 2019 - 2024. All rights reserved.