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

Jos*_*son 56 c# asp.net ssl httpwebrequest

我的客户通过他们的SSL和Internet Explorer了解了我的问题.他们说他们在访问URL时会遇到信任问题.

我通过HTTPS访问JSON.该网站位于一台服务器上,我在本地计算机上使用控制台应用程序.我试图绕过SSL证书,但是,我的代码仍然失败.

我可以改变HttpWebRequest来解决这个问题吗?

我使用此代码得到此错误:

    // You must change the URL to point to your Web server.
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "GET";
        req.AllowAutoRedirect = true;

        // allows for validation of SSL conversations
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


        WebResponse respon = req.GetResponse();
        Stream res = respon.GetResponseStream();

        string ret = "";
        byte[] buffer = new byte[1048];
        int read = 0;
        while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
        {
            //Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
            ret += Encoding.ASCII.GetString(buffer, 0, read);
        }
        return ret;
Run Code Online (Sandbox Code Playgroud)

glm*_*glm 134

我必须启用其他安全协议版本来解决此问题:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
        | SecurityProtocolType.Tls11
        | SecurityProtocolType.Tls12
        | SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)

  • @NicholasPetersen 仅供参考,这只能通过具有 Flags 属性的枚举来完成。请参阅 https://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx (2认同)

Jos*_*son 20

我使用此代码启用了日志记录:

http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx

日志位于bin/debug文件夹中(我的控制台应用程序处于调试模式).您需要将安全协议类型添加为SSL 3

我在日志中收到了算法不匹配的情况.这是我的新代码:

        // You must change the URL to point to your Web server.
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "GET";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;


        // Skip validation of SSL/TLS certificate
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };


        WebResponse respon = req.GetResponse();
        Stream res = respon.GetResponseStream();

        string ret = "";
        byte[] buffer = new byte[1048];
        int read = 0;
        while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
        {
            Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
            ret += Encoding.ASCII.GetString(buffer, 0, read);
        }
        return ret;
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,在SSLv3上发现了一个错误(http://security.stackexchange.com/a/70724),大多数实现都不再允许它.你应该使用`SecurityProtocolType.Tls12`代替. (9认同)
  • 请注意,使用此解决方案意味着*any*server证书将被接受为有效(因为`ServerCertificateValidationCallback`始终返回true) (9认同)

Cra*_*SFT 11

现有答案类似,但在PowerShell中:

[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.SecurityProtocolType]::Tls11 -bor 
[System.Net.SecurityProtocolType]::Tls12 -bor `   
[System.Net.SecurityProtocolType]::Tls -bor `
[System.Net.SecurityProtocolType]::Ssl3
Run Code Online (Sandbox Code Playgroud)

然后调用Invoke-WebRequest应该可以工作.

从匿名反馈,良好的建议得到这个:更简单的方式来写这个将是:

[System.Net.ServicePointManager]::SecurityProtocol = @("Tls12","Tls11","Tls","Ssl3")
Run Code Online (Sandbox Code Playgroud)

Jaykul发现了这篇精彩且相关的帖子:验证来自.Net和PowerShell的自签名证书


Jon*_*ner 6

这可能是由一些事情引起的(最有可能是最不可能的):

  1. 服务器的SSL证书不受客户端的信任.最简单的检查是将浏览器指向URL并查看是否获得SSL锁定图标.如果您获得了一个破锁,图标,请单击它以查看问题所在:

    1. 过期日期 - 获取新的SSL证书
    2. 名称不匹配 - 请确保您的URL使用与证书相同的服务器名称.
    3. 未由受信任的机构签名 - 从Verisign等机构购买证书,或将证书添加到客户的可信证书存储区.
    4. 在测试环境中,您可以更新证书验证程序以跳过访问检查.不要在生产中这样做.
  2. 服务器需要客户端SSL证书 - 在这种情况下,您必须更新代码以使用客户端证书对请求进行签名.

  • 除了最后一点,一切都很好。客户端证书不用于签署请求,它在握手中提供,因此需要在打开连接之前使其可用于传输。在 Javascript 中是如何完成的,留给读者作为练习;-) (2认同)

sam*_*air 6

对于.Net v4.0,我注意到,在创建 HttpWebRequest 对象之前将 ServicePointManager.SecurityProtocol 的值设置为 (SecurityProtocolType)3072会有所帮助。