Exception thrown when trying connect with FTPS using FtpWebRequest c#" - A call to SSPI failed, see inner exception"

1.7k Views Asked by At

I'm trying to update some FTP code I have as the server I'm connecting to is now forcing FTPS.

The exception I'm getting is as follows :

Message=A call to SSPI failed, see inner exception.

The code I'm using to connect is as follows:

public bool CheckFtpFile(string aFolderName, string aFileName)
{

    FtpWebRequest request;
    string absoluteFileName = Path.GetFileName(aFileName);


    request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}/{2}", ftpServer, aFolderName, absoluteFileName))) as FtpWebRequest;
    request.Credentials = new NetworkCredential(userName, passWord);
    request.EnableSsl = true;
    request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    request.UseBinary = true;
    request.UsePassive = true;

    //Line added as hack
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

    try
    {
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            return false;
        }
    }
    return true;
}

In order to ignore the cert, I'm using this:

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    System.Diagnostics.Debug.WriteLine(certificate);             
    return true;         
} 

Could someone explain what might be causing the exception?

Edit: The inner exception is showing as: {"The message received was unexpected or badly formatted"}

I'm currently using the .Net Franework 4.0 - could the problem be releated to the fact the server has been restricted to TLS 1.2 encription?

1

There are 1 best solutions below

0
On

You have to setup the Security protocol - before the (FtpWebRequest)FtpWebRequest.Create(fld);

Here I ignore ssl errors, and set the Security Protocol, and works.

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11;