当前位置:   article > 正文

The remote certificate is invalid according to the validation procedure

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).

  1. // C# version
  2. using (Ftp client = new Ftp())
  3. {
  4. // we will use custom validation
  5. client.ServerCertificateValidate +=
  6. new ServerCertificateValidateEventHandler(Validate);
  7. // Minimalistic version to accept any certificate:
  8. //client.ServerCertificateValidate +=
  9. // (sender, e) => { e.IsValid = true; };
  10. client.ConnectSSL("ftp.example.org");
  11. client.Login("username", "password");
  12. foreach (FtpItem item in client.GetList())
  13. {
  14. if (item.IsFolder == true)
  15. Console.WriteLine("[{0}]", item.Name);
  16. else
  17. Console.WriteLine"{0}", item.Name);
  18. }
  19. client.Close();
  20. }
  21. private static void ValidateCertificate(
  22. object sender,
  23. ServerCertificateValidateEventArgs e)
  24. {
  25. const SslPolicyErrors ignoredErrors =
  26. SslPolicyErrors.RemoteCertificateChainErrors | // self-signed
  27. SslPolicyErrors.RemoteCertificateNameMismatch; // name mismatch
  28. if ((e.SslPolicyErrors & ~ignoredErrors) == SslPolicyErrors.None)
  29. {
  30. e.IsValid = true;
  31. return;
  32. }
  33. e.IsValid = false;
  34. }

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



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/448803
推荐阅读
相关标签
  

闽ICP备14008679号