我正在尝试从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就会得到结果,如果我等待了一分钟左右,第一次也会出现同样的错误。
发布于 2019-11-12 03:45:23
在创建发出请求之前,需要设置安全协议类型。所以这就是:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;应出现在此之前:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);因此,如果您看到它在后续请求上起作用,可能是您设置协议太晚了。
发布于 2019-04-19 22:17:05
以下代码可用于帮助解决此问题。
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;
}发布于 2020-01-14 19:07:12
对于运行.NET版本4的用户,他们可以使用下面的
ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType) Or CType(3072,SecurityProtocolType)
ServicePointManager.Expect100Continue = Truehttps://stackoverflow.com/questions/55761221
复制相似问题