1

可能重复:
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;
        }
4

1 回答 1

0

我认为在您的代码中:

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

您正在添加一个证书验证器ValidateServerCertificate,覆盖您之前对 的分配ServerCertificateValidationCallback

尝试简单地使用:

SslStream sslStream = new SslStream(
        client.GetStream(),
        false
        );
于 2012-06-28T20:00:05.333 回答