你好,我正在尝试使用C#实现SSL客户端/服务器通信,并使用服务器和客户端证书进行相互认证。我已经成功地使用服务器证书完成了SSL通信,在客户端方面,我使用类似以下的内容:
TcpClient client = new TcpClient(machineName, port);
//Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
try
{
// The server name must match the name on the server certificate.
sslStream.AuthenticateAsClient(serverName);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
我假设我需要使用。
AuthenticateAsClient(string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
方法,我理解得对吗?有人能向我展示如何在所有事情中使用它吗?甚至在服务器端也可以,或者指向一个基本的例子?
非常感谢。