c# - RestRequest 在本地工作,但不能从 azure "The request was aborted: Could not create SSL/TLS secure channel."

标签 c# azure certificate restsharp

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

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

商业 API 已将我从 Azure 中的应用程序服务/属性/传出和虚拟 IP 地址获取的 IP 列入白名单 enter image description here

我已将客户端证书文件 (.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 安全通道。”

enter image description here

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

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

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

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

评论后的附加信息: enter image description here

最佳答案

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

关于c# - RestRequest 在本地工作,但不能从 azure "The request was aborted: Could not create SSL/TLS secure channel.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65899311/

相关文章:

c# - EF 竞争 SaveChanges() 调用

c# - 如何使泛型类的类型可转换?

Azure 应用程序洞察 | KQL|获取异常详细信息

azure - 从 Azure 搜索获取索引架构

node.js - 访问托管在不同端口的 WebSocket 服务器时出现混合内容错误

ssl - puppet + Apache + SSL

c# - 使用登录控件显示自定义错误消息时出现问题。

c# - 如何在 wpf 中使用没有 Windows Aero 玻璃效果的 WindowChrome,黑色边框

json - 如何使用 Azure 数据工厂管道将嵌套 JSON 对象映射到 SQL Server 表

push-notification - Apple Watch 的推送通知