c# - 在 Azure 函数中使用 RestSharp 调用 HTTPS 会给出 "The SSL connection could not be established"

标签 c# azure ssl azure-functions restsharp

如何使用特定证书从 Azure Function 中调用 HTTPS 站点来建立 HTTPS 连接?

我有一个 Azure 函数,需要使用证书与远程服务器进行通信。远程服务器是一家银行。

安装证书并在本地运行 Azure Function Emulator 将启动并建立连接。

上传到 Azure 我收到此消息

The SSL connection could not be established, see inner exception. Authentication failed, see inner exception. 

我尝试将证书包含在 Azure Function SLL 下 我将证书链中的所有证书安装为公共(public)证书 (.cer) 和私有(private)证书 (.pfx)

尝试通过指纹访问证书失败。

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
    {
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection certificates = store.Certificates;

        foreach (var certificate2 in certificates)
        {
            if (certificate2.Thumbprint.ToLower() == thumbprint.ToLower())
            {
                return certificate2;
            }
        }
     }

在这种情况下,我尝试太循环,而不是使用内置函数查找证书来使用 Tumbprint 查找证书

当这不起作用时,我提供了证书(.pfx)作为文件。

获取证书有效。

certificate = new X509Certificate2(filename, password, X509KeyStorageFlags.MachineKeySet);

Azure 函数需要 X509KeyStorageFlags.MachineKeySet,但运行本地 Azure 函数模拟器时不需要

RestSharp 正在 Azure Function Emulator 中运行

var client = new RestClient(BaseUrl)
{
    Timeout = 180000,
    ClientCertificates = new X509CertificateCollection() { cert }

};
client.AddDefaultHeader("Content-type", "application/json");
var request = new RestRequest(Query, Method.POST);
request.Parameters.Clear();
request.AddParameter("application/json", jsonSwish, ParameterType.RequestBody);
IRestResponse response = await client.ExecuteTaskAsync(request);

我收到“无法建立 SSL 连接...”

我在创建客户端之前添加了这个,但它没有解决问题

ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

然后,我创建了一个简单的 PHP,它在输入中包含 HTTP,并使用 CURL 和所需的证书作为 CURL 的参数创建 HTTPS 输出。 这可行,但是一个丑陋的解决方案。

如何使用特定证书从 Azure Function 中调用 HTTPS 站点来建立 HTTPS 连接?

最佳答案

根据描述,Web 应用程序中的代码向使用仅在您的网络中受信任的证书的后端服务发出 HTTPS 请求。由于 Web 应用程序服务器没有受信任的根证书,因此它会失败并出现“不受信任”错误。如果我误解了配置,请告诉我。

但是,以下信息基于该特定假设。

不幸的是,出于安全考虑,无法将证书导入到 Azure Web Apps 中的受信任根存储中。解决此错误的唯一选择是在应用程序代码中处理验证,类似于此处所述:https://stackoverflow.com/a/34204814 。我们过去已经成功地使用了这些步骤,例如:

  1. 创建新证书以获取要上传的 PFX 文件。
  2. 将 PFX 上传到 SSL 证书区域的应用服务
  3. 按照此链接中的步骤 1 和 2 加载证书:https://azure.microsoft.com/en-us/blog/using-certificates-in-azure-websites-applications/
  4. 添加上述链接中步骤 3 中的代码和此处代码的组合:Azure Web App calling on-prem service with Self-Signed SSL Cert 。本质上,从证书存储中获取证书,但从 ServerCertificateValidationCallback 函数中获取,然后如果证书匹配验证则返回 true。代码看起来像这样:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
            {
                if (sslPolicyErrors == SslPolicyErrors.None)
                {
                    return true;
                }
 
                var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                certStore.Open(OpenFlags.ReadOnly);
                var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, "THUMBPRINT", false);
 
                if (certCollection.Count > 0)
                {
                    var validCertificate = certCollection[0];
                    var passedCert = new X509Certificate2(certificate);
                    certStore.Close();
 
                    return validCertificate.Equals(passedCert);
                }
                certStore.Close();
                return false;
            };
 

关于c# - 在 Azure 函数中使用 RestSharp 调用 HTTPS 会给出 "The SSL connection could not be established",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55701263/

相关文章:

c# - 如何从 datagridview 中获取所有数据并将其插入数据库

azure - 我可以使用 Graph API 和 Azure ADB2C 通过原始登录屏幕对用户进行身份验证吗?

python - 创建名为 ssl.py 的 python 脚本后出现文件错误

java - 使用具有多个别名的单个证书配置 tomcat SSL

c# - Regex.Match 和非捕获组

c# - 带有客户端身份验证的 .Net 和 Java 之间的 SSL 套接字

azure - 如何正确授权Azure用户帐户以便它可以创建服务主体?

node.js - 将主动消息从 Azure 函数发送到机器人服务 - Node

Python SSL 套接字 : receiving and sending from both server and client

c# - 确定 XmlTextReader.Read() 是否读取结束标记