C# 跳过SSL TCP连接的验证过程
可能重复: 我正在编写通过SSL连接到远程服务器的代码。下面给出了我当前使用的代码。根据验证过程,我得到的远程证书无效 经过一些研究,我发现我必须跳过SSL设置的证书验证部分。我应该编写什么代码来跳过证书验证C# 跳过SSL TCP连接的验证过程,c#,.net,ssl,C#,.net,Ssl,可能重复: 我正在编写通过SSL连接到远程服务器的代码。下面给出了我当前使用的代码。根据验证过程,我得到的远程证书无效 经过一些研究,我发现我必须跳过SSL设置的证书验证部分。我应该编写什么代码来跳过证书验证 System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; T
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;
}
我认为在你的代码中:
SslStream sslStream = new SslStream(
client.GetStream(),
false,
ValidateServerCertificate,
null
);
您正在添加一个证书验证器ValidateServerCertificate,它将覆盖您以前对ServerCertificateValidationCallback的分配
试着简单地使用:
SslStream sslStream = new SslStream(
client.GetStream(),
false
);
经过几次研究,我发现我必须跳过SSL设置中的证书验证部分:您不必,您也可以做正确的事情并选择显式信任该特定证书。您能告诉我在代码中应该做什么吗?ValidateServerCertificate的定义是什么?公共静态bool ValidateServerCertificate对象发送方,X509Certificate证书,X509Chain,SslPolicyErrors SslPolicyErrors{if SslPolicyErrors==SslPolicyErrors.None返回true;Console.WriteLineCertificate错误:{0},SslPolicyErrors;//不允许此客户端与未经身份验证的服务器通信。返回true;}它正常工作。谢谢你的建议