无法通过C#连接到网站,但使用Firefox

问题描述 投票:0回答:1

[当我尝试连接到网站时出现错误:

请求已中止:无法创建SSL / TLS安全通道

异常中的状态为SecureChannelFailure,响应为null。

到目前为止,搜索StackOverflow的答案并没有帮助我。我尝试了所有解决方案:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

ServerCertificateValidationCallback永远不会被调用。

我可以在Firefox和Edge(Chromium)中正常打开网站,但Internet Explorer无法与其建立安全连接。因此,我假设HttpWebRequest使用的是与Internet Explorer相同的“机制”,但我不知道是哪一种。

关于https://server.cryptomix.com/secure/,我确保没有提供客户端SSL证书。这意味着我不必在请求中添加ClientCertificate。

这是我的整个测试代码:

public static void Main()
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    ServicePointManager.Expect100Continue = true;
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(@"https://netmap.vodafone.de"));

    request.Method      = "GET";
    request.ContentType = "text/json";

    try
    {
        using (var response = (HttpWebResponse)request.GetResponse())
        {

        }
    }
    catch (Exception e)
    {
        throw;
    }
}

您有什么想法,为什么我可以连接Internet Explorer和HttpWebRequest,但是可以使用Firefox?

运行代码时是否遇到相同的错误?

编辑:我发现该网站正在使用TLS 1.3,但是没有SecurityProtocolType.Tls13枚举值。

c# .net ssl
1个回答
0
投票

我以前在我的一个项目中有同样的问题。这为我解决了:

ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
           | SecurityProtocolType.Tls11
           | SecurityProtocolType.Tls12
           | SecurityProtocolType.Ssl3;

它需要System.Net命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.