Azure Web 作业无法与客户端证书建立 SSL/TLS 连接
Azure Web Job not able to establish SSL/TLS connection with client certificate
您好,我有一个 WebJob,它需要使用 HTTPS 和客户端证书与外部 Web 服务通信,但我收到(仅在 Azure 上)此错误:无法为 [ 建立安全通道=40=] 权限 'wstest.agenziadoganemonopoli.gov.it'。请求被中止:无法创建 SSL/TLS 安全通道。
wstest.agenziadoganemonopoli.gov.it 仅支持 TLS 1.2 (SSLLabs test),我已经添加了
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
wstest.agenziadoganemonopoli.gov.it 每个用户需要不同的客户端证书,因此我从 blob 加载了正确的证书。
我没有按照此处的建议使用 WEBSITE_LOAD_CERTIFICATES 主要是因为我从外部存储(Azure Blob 存储)加载证书并且我每个用户有不同的证书(我有 >1000 个证书要管理)。
更新
这是(或多或少)我用来从 Azure Blob 存储加载证书的代码
public async Task<Result<X509Certificate2>> GetCertificate(Guid merchantId)
{
try
{
CloudBlobContainer container =
_client.GetContainerReference($"{ContainerNamePrefix}-{merchantId.ToString().ToLower()}");
CloudBlockBlob certificateBlobBlock = container.GetBlockBlobReference(CertificateBlobBlockName);
CloudBlockBlob metadataBlobBlock = container.GetBlockBlobReference(MetadataBlobBlockName);
AuthenticationCertificateMetadata metadata =
JsonConvert.DeserializeObject<AuthenticationCertificateMetadata>(
await metadataBlobBlock.DownloadTextAsync().ConfigureAwait(false));
X509Certificate2 certificate = null;
using (MemoryStream stream = new MemoryStream())
{
await certificateBlobBlock.DownloadToStreamAsync(stream).ConfigureAwait(false);
certificate = new X509Certificate2(stream.ToArray(), metadata.CertificatePassword);
}
return Result<X509Certificate2>.Successful(certificate);
}
catch (Exception ex)
{
}
}
有什么建议吗?
谢谢!
费德里科
I didn't use WEBSITE_LOAD_CERTIFICATES as suggested
实际上,您需要在应用程序设置中设置WEBSITE_LOAD_CERTIFICATES。这是加载应用程序的客户端证书所必需的。
I load the certificate from an external store (Azure Blob Storage) and I have a different certificate per user
您可以加载应用程序目录中的证书文件,这样您就可以为每个用户获得不同的客户端证书。
例如:
string certPath = Server.MapPath("~/certs/mycert.pfx");
X509Certificate2 cert = GetCertificate(certPath, signatureBlob.Thumbprint);
更多细节,你可以参考这个article。
您好,我有一个 WebJob,它需要使用 HTTPS 和客户端证书与外部 Web 服务通信,但我收到(仅在 Azure 上)此错误:无法为 [ 建立安全通道=40=] 权限 'wstest.agenziadoganemonopoli.gov.it'。请求被中止:无法创建 SSL/TLS 安全通道。
wstest.agenziadoganemonopoli.gov.it 仅支持 TLS 1.2 (SSLLabs test),我已经添加了
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
wstest.agenziadoganemonopoli.gov.it 每个用户需要不同的客户端证书,因此我从 blob 加载了正确的证书。
我没有按照此处的建议使用 WEBSITE_LOAD_CERTIFICATES
更新 这是(或多或少)我用来从 Azure Blob 存储加载证书的代码
public async Task<Result<X509Certificate2>> GetCertificate(Guid merchantId)
{
try
{
CloudBlobContainer container =
_client.GetContainerReference($"{ContainerNamePrefix}-{merchantId.ToString().ToLower()}");
CloudBlockBlob certificateBlobBlock = container.GetBlockBlobReference(CertificateBlobBlockName);
CloudBlockBlob metadataBlobBlock = container.GetBlockBlobReference(MetadataBlobBlockName);
AuthenticationCertificateMetadata metadata =
JsonConvert.DeserializeObject<AuthenticationCertificateMetadata>(
await metadataBlobBlock.DownloadTextAsync().ConfigureAwait(false));
X509Certificate2 certificate = null;
using (MemoryStream stream = new MemoryStream())
{
await certificateBlobBlock.DownloadToStreamAsync(stream).ConfigureAwait(false);
certificate = new X509Certificate2(stream.ToArray(), metadata.CertificatePassword);
}
return Result<X509Certificate2>.Successful(certificate);
}
catch (Exception ex)
{
}
}
有什么建议吗?
谢谢! 费德里科
I didn't use WEBSITE_LOAD_CERTIFICATES as suggested
实际上,您需要在应用程序设置中设置WEBSITE_LOAD_CERTIFICATES。这是加载应用程序的客户端证书所必需的。
I load the certificate from an external store (Azure Blob Storage) and I have a different certificate per user
您可以加载应用程序目录中的证书文件,这样您就可以为每个用户获得不同的客户端证书。
例如:
string certPath = Server.MapPath("~/certs/mycert.pfx");
X509Certificate2 cert = GetCertificate(certPath, signatureBlob.Thumbprint);
更多细节,你可以参考这个article。