使用以下 TcpClient 代码,sslStream 的身份验证正常。
X509Certificate serverCertificate = new X509Certificate("PathToMyCert\MyCert.pfx", "CertPassword");
TcpListener listener = new TcpListener(IPAddress.Any, 9001);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
SslStream sslStream = new SslStream(client.GetStream(), false);
sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls12, true);
Console.WriteLine("Authenticated...");
但是,使用套接字的等效代码,我得到一个 System.NotSupportedException:“服务器模式 SSL 必须使用具有关联私钥的证书。”在 sslStream.AuthenticateAsServer 上,但它是相同的证书等。下面是我的套接字代码的近似值。关于我做错了什么的任何想法?
X509Certificate serverCertificate = new X509Certificate("PathToMyCert\MyCert.pfx", "CertPassword");
IPEndPoint _endPoint = new IPEndPoint(IPAddress.Any, 9001);
Socket listener = new Socket(SocketType.Stream, ProtocolType.Tcp);
listener.Bind(_endPoint);
listener.Listen(10);
Socket socket = listener.Accept();
NetworkStream innerStream = new NetworkStream(socket);
SslStream sslStream = new SslStream(innerStream, false);
sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls12, true);
Console.WriteLine("Authenticated...");