zoukankan      html  css  js  c++  java
  • (转)The remote certificate is invalid according to the validation procedure

    If you get “The remote certificate is invalid according to the validation procedure” exception while trying to establish SSL connection, most likely your’s server certificate is self-signed or you used incorrect host name to connect (Host name must match the name on certificate, for example ftp.example.com and example.com may point to the same server, but certificate is issued only to ftp.example.com and this is the address you should use).

    Good news is that you can accept self-signed certificates using Ftp.dll FTP and FTPS .NET component.

    First you need to subscribe to ServerCertificateValidate event.

    Then you need to create ValidateCertificate method that validates the certificate (ignores certificate chain and name mismatch errors).

    // C# version
    
    using (Ftp client = new Ftp())
    {
        // we will use custom validation
        client.ServerCertificateValidate +=
            new ServerCertificateValidateEventHandler(Validate);
    
        // Minimalistic version to accept any certificate:
        //client.ServerCertificateValidate += 
        //    (sender, e) => { e.IsValid = true; };
    
        client.ConnectSSL("ftp.example.org");
        client.Login("username", "password");
    
        foreach (FtpItem item in client.GetList())
        {
            if (item.IsFolder == true)
                Console.WriteLine("[{0}]", item.Name);
            else
                Console.WriteLine"{0}", item.Name);
        }
        client.Close();
    }
    
    private static void ValidateCertificate(
        object sender,
        ServerCertificateValidateEventArgs e)
    {
        const SslPolicyErrors ignoredErrors =
            SslPolicyErrors.RemoteCertificateChainErrors |  // self-signed
            SslPolicyErrors.RemoteCertificateNameMismatch;  // name mismatch
    
        if ((e.SslPolicyErrors & ~ignoredErrors) == SslPolicyErrors.None)
        {
            e.IsValid = true;
            return;
        }
        e.IsValid = false;
    }

    You can download Ftp.dll FTP/FTPS component for .NET here.

  • 相关阅读:
    HDU 1394 Minimum Inversion Number
    LA 3938 动态最大连续和(线段树)
    HDU 1754 I Hate It(线段树)
    HDU 1166 敌兵布阵(线段树 or 二叉索引树)
    《乞力马扎罗的雪》读书笔记
    LA 3266 田忌赛马
    UVa 11235 频繁出现的数值
    《月亮与六便士》读书笔记
    LA 3135 阿格斯(优先队列)
    LA 3027 合作网络
  • 原文地址:https://www.cnblogs.com/s021368/p/3190481.html
Copyright © 2011-2022 走看看