The following piece of code (copy/paste ready, just replace the IP/port by something valid) throw an ArgumentNullException
(nothing helpful in the stacktrace) on the line 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;
}
}
}
Why does it throw an exception? No parameter is null. The strange thing is that it only throws the exception when targeting framework .Net 4.6 or 4.6.1. It runs perfectly fine with .Net 4.5, 4.5.1 or 4.5.2.
Is this a framework bug?
EDIT:
In case you want to test, you could run the following code in another Console project:
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();
}
}
}
Then use 127.0.0.1 / 8883 as IP / port on the client code side.