none
Cannot connect to a FTPS(SSL/TLS) server RRS feed

  • Вопрос

  • Hello all. I have spent a day long trying to figure out why I could not connect to a FTPS server but to no avail. I always got this error.

    System.Security.Authentication.AuthenticationException
    a call to SSPI failed, see inner exception

    I set up a connection in FileZilla, with protocal=FTP and encription=any of the three TLS choices. It works well. I can download, upload and delete files in the server. But it just doesn't work with C# code.

    Does anyone have any ideas ?

    public bool CheckConnection(string RemoteHost, string UserName, string Password)
    {
         try
         {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemoteHost);
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential(UserName, Password);             
            request.GetResponse();
          }
            catch (WebException ex)
          {
             MessageBox.Show("Fail:" + ex.ToString());
              return false;
          }
           return true;
    }



    • Изменено AlanChong 19 мая 2020 г. 3:58

Ответы

  • Hi Zhao. Finally, X509Certificate came to rescue. I happened to look at this page

    https://www.codeproject.com/Questions/208700/solve-error-the-remote-certificate-is-invalid-acco

    I added one critical line and it connected to the FTPS server successfully, working like magic.

    using System.Net.Security;  // 使用 X509Certificate 須引用這兩行
    using System.Security.Cryptography.X509Certificates;

    // after EnableSsl=true;
    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };


Все ответы

  • Certainly I have an idea. Look at the inner exception. Also, what is the exception code? Search the internet for the exception code and for other relevant terms such as "ftp". Also are you sure that ftp is correct? Are you using ftps instead? I vaguely remember that there are version problems with TLS and doing the search I describe will likely find stuff about that. Don't be surprised if the inner exception says something about one of the preceding.


    Sam Hobbs
    SimpleSamples.Info

  • Hello. Yes, I am using FTPS. I suppose it is that simple to just set request.EnableSsl to true for FTPS to work. My .net framewok is version 4.5.2. No version problem. Ip, username and password are all correct because I keep them in a text file. I created a new FileZilla connection and copy-and-paste those there to FileZilla. There was no typing error. I could connect to the server throuhg FileZilla. My VS is a Chinese Version so the error dialog was in part in Chinese. The second Chinese sentence is "The received data was unexpected or the format is incorrect", I used the same function to connect to ftp://test.rebex.net which accepts both regular FTP and FTPS. I could connect to this server with or without EnableSsl

  • Hi AlanChong,

    Thank you for posting here.

    Problem may be related to the version of TLS, because TLS 1.2 is not supported on .Net 4.0:

    How to enable TLS 1.2 on clients

    Here's a related reference about your exception, and you can refer to the suggestions in it:

    A call to SSPI failed, see inner exception - The Local Security Authority cannot be contacted

    Besides, FtpWebRequest is not recommended for new development, and you can try other free FTP libraries:

    Free FTP Library

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Модератор
  • Hi Zhao. I visited one of the links you gave and add this line. The new error seems to be more helpful.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

  • Hi Zhao, What ftp should I use instead of FtpWebRequest ? I just finished a new program using SFTP through Renci.SshNet. Can I also use Renci.SshNet to connect to a FTPS server ?
  • Hi AlanChong,

    Thanks for your feedback.

    >>Can I also use Renci.SshNet to connect to a FTPS server ?

    No, you cannot use the Renci SSH.NET for the FTP protocol. Renci SSH.NET is an SSH library which has nothing to do with FTP.

    Differences between SFTP and “FTP over SSH”

    >> What ftp should I use instead of FtpWebRequest ? 

    You can consider using FluentFTP.

    Check the code in following reference:

    FTPS Connection in FluentFTP

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Модератор
  • Hi Zhao. Finally, X509Certificate came to rescue. I happened to look at this page

    https://www.codeproject.com/Questions/208700/solve-error-the-remote-certificate-is-invalid-acco

    I added one critical line and it connected to the FTPS server successfully, working like magic.

    using System.Net.Security;  // 使用 X509Certificate 須引用這兩行
    using System.Security.Cryptography.X509Certificates;

    // after EnableSsl=true;
    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };


  • Hi AlanChong,

    I'm glad to hear that your problem has been solved,and I suggest you click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Модератор
  • Hi Zhao. I didn't know I could mark my own posts. Thanks.