Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Last active May 22, 2017 04:48
Show Gist options
  • Save EgorBo/6c353750be36c65f493660a53b114057 to your computer and use it in GitHub Desktop.
Save EgorBo/6c353750be36c65f493660a53b114057 to your computer and use it in GitHub Desktop.
//STEP 1: open a socket and network streams
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("localhost", 1433);
var tcpStream = new NetworkStream(socket, true);
var sslOverTdsStream = new SslOverTdsStream(tcpStream); //simple Stream wrapper - https://github.com/dotnet/corefx/blob/master/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs
var sslStream = new SslStream(sslOverTdsStream, leaveInnerStreamOpen: true, userCertificateValidationCallback: new RemoteCertificateValidationCallback(ValidateServerCertificate), userCertificateSelectionCallback: null);
//STEP 2: send & receive data via tcpStream (not ssl)
tcpStream.Write...
tcpStream.Read...
//STEP 3: send & receive data via sslStream
sslStream.AuthenticateAsClientAsync(_targetServer);
sslOverTdsStream.FinishHandshake();
sslOverTdsStream.Write...
sslOverTdsStream.Read...
//STEP 4: Dipose sslStream & sslOverTdsStream (it should not dispose tcpStream);
sslOverTdsStream.Dispose();
sslStream.Dispose();
//STEP 5: Send & receive data via tcpStream
tcpStream.Write...
tcpStream.Read... -- hangs here. but if comment sslStream.Dispose() -- it won't hang.
//Same code works in corefx on both macOS and Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment