首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第一次命中API时,“请求已中止:无法创建SSL/TLS安全通道”

第一次命中API时,“请求已中止:无法创建SSL/TLS安全通道”
EN

Stack Overflow用户
提问于 2019-04-19 19:35:36
回答 3查看 26.8K关注 0票数 3

我正在尝试从Web API使用客户端的Web服务,下面是我们当前用来绕过SSL证书的代码

ServicePointManager.ServerCertificateValidationCallback += (发送者,证书,链,sslPolicyErrors) => true;

它工作得很好,直到他们禁用了TLS 1.0和TLS 1.1。现在,我们添加了以下代码,以便使用TLS 1.2进行客户端和服务器连接

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;发送方+= (发送方,证书,链,sslPolicyErrors) => true;

现在我看到“请求已中止:无法创建SSL/TLS安全通道”。只有当我第一次点击API时才会出错,如果我连续点击API就会得到结果,如果我等待了一分钟左右,第一次也会出现同样的错误。

EN

回答 3

Stack Overflow用户

发布于 2019-11-12 03:45:23

在创建发出请求之前,需要设置安全协议类型。所以这就是:

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

应出现在此之前:

代码语言:javascript
复制
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

因此,如果您看到它在后续请求上起作用,可能是您设置协议太晚了。

票数 11
EN

Stack Overflow用户

发布于 2019-04-19 22:17:05

以下代码可用于帮助解决此问题。

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;

...

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    // If the certificate is a valid, signed certificate, return true to short circuit any add'l processing.
    if (sslPolicyErrors == SslPolicyErrors.None)
    {
        return true;
    }
    else
    {
        // cast cert as v2 in order to expose thumbprint prop - if needed
        var requestCertificate = (X509Certificate2)certificate;

        // init string builder for creating a long log entry
        var logEntry = new StringBuilder();

        // capture initial info for the log entry
        logEntry.AppendFormat("SSL Policy Error(s): {0} - Cert Issuer: {1} - SubjectName: {2}",
           sslPolicyErrors.ToString(),
           requestCertificate.Issuer,
           requestCertificate.SubjectName.Name);

        // check for other error types as needed
        if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors) //Root CA problem
        {
            // check chain status and log
            if (chain != null && chain.ChainStatus != null)
            {
                // check errors in chain and add to log entry
                foreach (var chainStatus in chain.ChainStatus)
                {
                    logEntry.AppendFormat("|Chain Status: {0} - {1}", chainStatus.Status.ToString(), chainStatus.StatusInformation.Trim());
                }
            }
        }

        // replace with your logger
        MyLogger.Info(logEntry.ToString().Trim());
    }

    return false;
}
票数 3
EN

Stack Overflow用户

发布于 2020-01-14 19:07:12

对于运行.NET版本4的用户,他们可以使用下面的

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType) Or CType(3072,SecurityProtocolType)
ServicePointManager.Expect100Continue = True
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55761221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档