可能重复:
C#忽略证书错误?
我正在编写代码以通过 SSL 连接到远程服务器。我目前使用的代码如下。我收到“根据验证程序,远程证书无效。”
经过一番研究,我发现我必须跳过 SSL setup 的证书验证部分。我应该写什么代码来跳过证书验证?
System.Net.ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, errors) => true;
TcpClient client = new TcpClient("IPADDDRESSHERE", 80);
Console.WriteLine("Client connected.");
SslStream sslStream = new SslStream(
client.GetStream(),
false,
ValidateServerCertificate,
null
);
try
{
sslStream.AuthenticateAsClient("IPADDDRESSHERE");
}
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;
}