.net 错误:无法创建SSL/TLS安全通道Windows 8

.net 错误:无法创建SSL/TLS安全通道Windows 8,.net,ssl,httpwebrequest,windows-8.1,.net,Ssl,Httpwebrequest,Windows 8.1,升级到Windows 8后,我遇到了一个以前有效的web服务调用问题。我已经在两台Windows 8.1计算机和一台Windows 8计算机上验证了以下代码失败,但它在Windows 7和Windows Server 2008 R2上正常工作 var uriString = "https://secure.unitedmileageplus.com/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);

升级到Windows 8后,我遇到了一个以前有效的web服务调用问题。我已经在两台Windows 8.1计算机和一台Windows 8计算机上验证了以下代码失败,但它在Windows 7和Windows Server 2008 R2上正常工作

var uriString = "https://secure.unitedmileageplus.com/";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);

try {
    using(WebResponse response = request.GetResponse()) {
        response.Dump();
    }
}
catch(Exception e) {
    e.Dump();
}
WebException请求被中止:无法创建SSL/TLS安全 频道


它似乎被本地化到此端点,因为我能够成功地对其他URL进行SSL调用。我已经做了一些线鲨嗅探,但不知道要寻找什么,这没有多大帮助。如果您希望我也提供这些日志,请告诉我。

WebRequest
默认情况下将TLS/SSL版本设置为TLS 1.0。您可以使用
ServicePointManager.SecurityProtocol
将其设置回SSL 3.0。例如:

static void Main(string[] args)
{
    var uriString = "https://secure.unitedmileageplus.com/";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);

    ServicePointManager.ServerCertificateValidationCallback =
        new RemoteCertificateValidationCallback(AcceptAllCertifications);

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

    try
    {
        using (WebResponse response = request.GetResponse())
        {
            Debug.WriteLine(response);
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

public static bool AcceptAllCertifications(
    object sender,
    System.Security.Cryptography.X509Certificates.X509Certificate certification,
    System.Security.Cryptography.X509Certificates.X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
{
    return true;
}

WebRequest
默认情况下将TLS/SSL版本设置为TLS 1.0。您可以使用
ServicePointManager.SecurityProtocol
将其设置回SSL 3.0。例如:

static void Main(string[] args)
{
    var uriString = "https://secure.unitedmileageplus.com/";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);

    ServicePointManager.ServerCertificateValidationCallback =
        new RemoteCertificateValidationCallback(AcceptAllCertifications);

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

    try
    {
        using (WebResponse response = request.GetResponse())
        {
            Debug.WriteLine(response);
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

public static bool AcceptAllCertifications(
    object sender,
    System.Security.Cryptography.X509Certificates.X509Certificate certification,
    System.Security.Cryptography.X509Certificates.X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
{
    return true;
}

SSL3已被弃用,因此使用时要小心!SSL3已被弃用,因此使用时要小心!