【问题标题】:Consuming webservice that requires Client Certificate and Root Certificate using C# Restsharp Library使用 C# Restsharp 库使用需要客户端证书和根证书的 web 服务
【发布时间】:2022-02-03 17:20:07
【问题描述】:

我正在尝试使用要求开发人员将签名证书作为参数的一部分传递的 Web 服务。该服务在测试环境中运行良好,但是对于生产环境,除了签名证书之外,您还需要根证书才能成功访问该服务。这已经在 Postman 中进行了测试,并产生了成功的结果。

当这在 C# 代码中使用 RestSharp 库实现时,我会得到如下所示的响应。

查询客户端响应日志: {"statusCode":0,"statusDescription":null,"content":"","headers":[],"responseUri":null,"errorMessage":"The 请求被中止:无法创建 SSL/TLS 安全通道。"}

我的问题是,我如何使用 Restsharp 库在 C# 中实现这一点。下面是我实现这一目标的代码。但是我不断收到错误“请求被中止:无法创建 SSL/TLS 安全通道。

log.Info("--------------------发起查询请求---------- ----"); QueryClient 广告 = new QueryClient(); ad.institutionId = ConfigurationManager.AppSettings["OriginInst"]; ad.proxyId = pr.proxyId; ad.requestSource = "XX"; ad.requestTimestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 字符串 concat = ad.institutionId + ad.proxyId + ad.requestSource + ad.requestTimestamp; HelperLibrary hl = new HelperLibrary(); 字符串键 = ConfigurationManager.AppSettings["pkey2"]; 字符串签名 = hl.GetSignature(concat, key); ad.requestSignature = 签名; ServicePointManager.Expect100Continue = true; ServicePointManager.DefaultConnectionLimit = 9999; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |安全协议类型.Tls11 |安全协议类型.Tls12 |安全协议类型.Ssl3; var client = new RestSharp.RestClient("https://service.url"); //加载证书 var myCert = new X509Certificate2(ConfigurationManager.AppSettings["certificatePath"], ConfigurationManager.AppSettings["certificatePassword"], X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); var rootCert = new X509Certificate2(ConfigurationManager.AppSettings["certificateRootPath"]); X509Chain 链 = 新 X509Chain(); 链.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; 链.ChainPolicy.ExtraStore.Add(rootCert); X509CertificateCollection clientCerts = new X509CertificateCollection(); clientCerts.Add(myCert); clientCerts.Add(rootCert); client.ClientCertificates = clientCerts; ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; }); var request = new RestSharp.RestRequest(RestSharp.Method.POST); log.Info("使用证书路径查询客户端:" + ConfigurationManager.AppSettings["certificatePath"]); request.AddHeader("接受", "应用程序/json"); request.AddHeader("content-type", "application/json"); request.AddParameter("application/json", jsonString, RestSharp.ParameterType.RequestBody); var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var requestToLog = 新 { 资源 = 请求。资源, 参数 = serializer.Serialize(request.Parameters), 方法 = request.Method.ToString(), // 这将生成请求中使用的实际 Uri uri = client.BuildUri(request), }; log.Info("查询客户端请求:" + requestToLog); RestSharp.IRestResponse 响应 = client.Execute(request); var responseToLog = 新 { statusCode = response.StatusCode, statusDescription = response.StatusDescription, 内容=响应。内容, 标头=响应。标头, responseUri = response.ResponseUri, errorMessage = response.ErrorMessage, }; log.Info("查询客户端响应日志:" + JsonConvert.SerializeObject(responseToLog));

您的建议将不胜感激。

【问题讨论】:

  • 您可能需要将 SecurityProtocolType 设置为 Tls。
  • 在初始 HTTPS 发生之前发生的 TLS 验证失败。 Net 4.7.2 默认情况下,TLS 在操作系统中执行,而不是在 Net 应用程序中执行。所以我不认为错误是因为发布的代码。 TLS 验证服务器发送一个证书块,其中包含可以使用的证书名称。客户端根据客户端存储中加载的证书检查证书名称。听起来错误是由于证书不在商店中。使用像wireshark这样的嗅探器,它可以读取证书块来获取名称。
  • @jdweng 你是对的。该错误与代码无关。我必须在浏览器中添加根证书,这解决了问题。

标签: c# ssl restsharp


【解决方案1】:

错误与代码无关。出现此问题是因为我正在从浏览器访问该服务。然而,浏览器并没有这个证书颁发机构的任何记录。必须将根证书添加到浏览器的证书管理器中。以下是在 Firefox 浏览器中添加证书颁发机构的步骤。

工具->设置->隐私和安全->查看证书->权限->导入

First Screenshot on how to add certificate authority in Firefox Browser

Second Screenshot on how to add certificate authority in Firefox Browser

添加证书后,我就可以顺利访问服务了。

【讨论】:

    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2020-04-14
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多