1

重複の可能性:
C# 証明書エラーを無視しますか?

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;
        }
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 に答える