public AuthenticateAsClientAsync ( string targetHost ) : System.Threading.Tasks.Task | ||
targetHost | string | |
return | System.Threading.Tasks.Task |
This code demonstrates how to establish a secure connection with a server using SSL/TLS encryption over port 443. The SslStream is created using the TcpClient's network stream, then AuthenticateAsClientAsync is called with the name of the server to initiate a secure handshake. Example 2:csharp using System; using System.Net; using System.Net.Security; using System.Net.Sockets; public class SslClient { private TcpClient client; private SslStream sslStream; public void Connect(string serverName, int port) { client = new TcpClient(serverName, port); sslStream = new SslStream(client.GetStream(), false, (sender, certificate, chain, sslPolicyErrors) => true); sslStream.AuthenticateAsClientAsync(serverName).Wait(); } public string Send(string request) { byte[] buffer = System.Text.Encoding.ASCII.GetBytes(request); sslStream.Write(buffer); sslStream.Flush(); buffer = new byte[client.ReceiveBufferSize]; int bytesRead = sslStream.Read(buffer, 0, client.ReceiveBufferSize); string response = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); return response; } public void Disconnect() { sslStream.Close(); client.Close(); } } ``` This code provides an example of a simple SSL client class that can connect to a server, send requests, and receive responses. The constructor of the class initializes the TcpClient and SslStream, sets the SSL policy to ignore certificate validation errors, and calls AuthenticateAsClientAsync to establish the secure connection. The Send method sends a request to the server and receives the response, and the Disconnect method closes the SSL stream and TCP client connections. This code likely belongs to "System.Net.Security" NuGet package.
public AuthenticateAsClientAsync ( string targetHost ) : System.Threading.Tasks.Task | ||
targetHost | string | |
return | System.Threading.Tasks.Task |