跳过SSL TCP连接的验证过程

时间:2012-06-28 19:37:37

标签: c# .net ssl

  

可能重复:
  C# Ignore certificate errors?

我正在编写代码以通过SSL连接到远程服务器。我目前使用的代码如下。我得到“根据验证程序,远程证书无效。”

经过一些研究后,我发现我必须跳过SSL设置的证书验证部分。我应该写什么代码来跳过证书验证?

        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;
        }

1 个答案:

答案 0 :(得分:0)

我认为你的代码在这里:

SslStream sslStream = new SslStream(
        client.GetStream(),
        false,
        ValidateServerCertificate,
        null
        );

您正在添加证书验证程序ValidateServerCertificate,将您先前的作业覆盖为ServerCertificateValidationCallback

尝试使用:

SslStream sslStream = new SslStream(
        client.GetStream(),
        false
        );