RestRequest在本地工作,但不是从azure工作“请求已中止:无法创建SSL/TLS安全通道。”

c#

当我在 localhost 中运行我的应用程序时,它工作正常,但是当我将它发布到 Azure 时,我的请求停止工作。收到错误消息:“请求已中止:无法创建 SSL/TLS 安全通道。”

我有一个调用外部商业 Soap-API 的应用程序。外部 API 需要在我发出请求时传递客户端证书,并且还需要将我的 IP 地址列入白名单。

商业 API 已将我从 Azure 中的应用服务/属性/传出和虚拟 IP 地址获得的 IP 列入白名单

我已将我的客户端证书文件 (.p12) 添加到我的解决方案中的文件夹中,并且在检查上传到 azure 的文件时,我也可以在那里看到它。

使用 RestSharp,我的请求看起来像:

   private string RequestToBv(string pXml)
    {            
        X509Certificate2 cert = new X509Certificate2(bvCertificatePath, bvCertificatePassword);

        var client = new RestClient(mXmlApiUrl); //mXmlApiUrl = url to endpoint
        client.Timeout = -1;
        client.ClientCertificates = new X509CertificateCollection() { cert };
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/xml");
        request.AddParameter("application/xml", pXml, ParameterType.RequestBody);

        IRestResponse response = client.Execute(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            return response.Content;
        }

        return "";
    }

在 Azure 中调试时,我收到 StatusCode = 0 和错误消息:“请求已中止:无法创建 SSL/TLS 安全通道。”

在 stackoverflow 中搜索答案后,我必须在我的方法顶部添加以下代码行:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;

但我仍然得到相同的错误响应。

是否需要在 Azure 中设置任何设置或在 Azure 上以某种方式安装我的客户端证书?

评论后的补充信息:

回答

我必须做的是将我的 .p12 证书文件转换为 crt 文件,将其导入 Azure,然后在我的代码中使用 X509Store 来获取它。之后握手成功


以上是RestRequest在本地工作,但不是从azure工作“请求已中止:无法创建SSL/TLS安全通道。”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>