SslStream使用.Net 4.6抛出ArgumentNullException

时间:2016-10-21 15:50:28

标签: c# .net ssl .net-4.6 sslstream

下面的一段代码(复制/粘贴就绪,只需用有效的东西替换IP /端口)在行ArgumentNullException上抛出sslStream.AuthenticateAsClient(在堆栈跟踪中没什么用处):

namespace Test
{
    using System;
    using System.Net.Security;
    using System.Net.Sockets;
    using System.Security.Cryptography.X509Certificates;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new TcpClient("VALID_IP_HERE", VALID_PORT_HERE);

            using (var sslStream = new SslStream(client.GetStream(), false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
            {
                // No parameter is null here
                sslStream.AuthenticateAsClient(
                    "some_string_here",
                    new X509CertificateCollection(),
                    System.Security.Authentication.SslProtocols.Tls12,
                    true);    
            }

            client.Close();
        }

        public static bool ValidateServerCertificate(object sender, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }
}

good ol' exception here

为什么会抛出异常?没有参数为空。奇怪的是,它只在针对框架.Net 4.6或4.6.1时抛出异常。它与.Net 4.5,4.5.1或4.5.2完全匹配。

这是一个框架错误吗?

编辑:

如果您想测试,可以在另一个Console项目中运行以下代码:

namespace TestServer
{
    using System;
    using System.Net;
    using System.Net.Sockets;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var listener = new TcpListener(IPAddress.Any, 8883);
            listener.Start();
            var clientSocket = listener.AcceptTcpClient();

            // Nothing more here for the sake of simplicity

            Console.ReadKey();
        }
    }
}

然后在客户端代码端使用127.0.0.1 / 8883作为IP /端口。

0 个答案:

没有答案