Here is the Error:
This operation is valid only when using a context that has been authenticated successfully. Here is the Server Code:

using System.Net.Security; internal static SslStream sslStream; tcpListener = new TcpListener(IPAddress.Any, 8000); tcpListener.Start(); TcpClient сlient = tcpListener.AcceptTcpClient(); Stream = client.GetStream(); sslStream = new SslStream(Stream); string message = Console.ReadLine(); byte[] data = Encoding.Unicode.GetBytes(message); sslStream.Write(data, 0, data.Length); 

Client code:

 using System.Net.Security; internal static SslStream sslStream; client = new TcpClient(); client.Connect("127.0.0.1", 8000); stream = client.GetStream(); sslStream = new SslStream(stream); sslStream.AuthenticateAsClient("127.0.0.1"); string message = userName; byte[] data = new byte[1024]; sslStream.Read(data, 0, data.Length); Console.WriteLine(Encoding.Unicode.GetString(data, 0, bytes)); 

    1 answer 1

    You can't just start reading / writing with SslStream , you first need to call one of the methods: AuthenticateAsClient / AuthenticateAsServer . On the server side, you do not call anything, so you get an error.

    In order for everything to work, you need to create / get a certificate.

    Minimum working example:

     static void Main(string[] args) { var serverThread = new Thread(() => { var server = new TcpListener(IPAddress.Any, 12345); server.Start(); var client = server.AcceptTcpClient(); var ssl = new SslStream(client.GetStream()); var cert = new X509Certificate2(@"rsa-4096.pfx", "hh87$-Jqo"); ssl.AuthenticateAsServer(cert); ssl.Write(Encoding.ASCII.GetBytes("Hello world"), 0, 11); ssl.Flush(); ssl.Close(); server.Stop(); }); var clientThread = new Thread(() => { var client = new TcpClient(); client.Connect(IPAddress.Loopback, 12345); // последний параметр отключает проверку серверного сертификата var ssl = new SslStream(client.GetStream(), false, (a, b, c, d) => true); ssl.AuthenticateAsClient("localhost"); using (var sr = new StreamReader(ssl, Encoding.ASCII)) { string recivedText = sr.ReadToEnd(); Console.WriteLine(recivedText); } }); serverThread.Start(); clientThread.Start(); serverThread.Join(); clientThread.Join(); } 

    Certificate for example: https://github.com/Zergatul/ZergatulLib/blob/master/ConsoleTest/rsa-4096.pfx

    • Maybe write a little more detail? Imhu, at the moment somehow too brief and incomprehensible. - AK
    • I know that I need to call this method, but I do not know what needs to be passed in the arguments. I looked in the documentation and on MDSN and stackoverflow but I did not understand where to get the ssl certificate; - Gweston 2:57 pm
    • @Gweston I updated the answer, you can search for how to create certificates and what it is. If something does not work, ask a new question - Zergatul
    • stackoverflow.com/questions/10175812/… example how to create a certificate for your case using openssl - Zergatul
    • I understood how to create, but how to connect, I realized that I need to create a certificate in the project file, and use System.IO.File.ReadAllText (file path) in a variable to transfer to AuthenticateAsServer - Gweston pm