I am using System.Net.FtpClient to download FTP files. I've run into an odd issue on Windows Server 2012, where I am getting the following exception:
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted\r\n --- End of inner exception stack trace ---\r\n at System.Net.FtpClient.FtpSocketStream.ActivateEncryption(String targethost, X509CertificateCollection clientCerts)\r\n at System.Net.FtpClient.FtpClient.OpenPassiveDataStream(FtpDataConnectionType type, String command, Int64 restart)\r\n at System.Net.FtpClient.FtpClient.OpenDataStream(String command, Int64 restart)\r\n at System.Net.FtpClient.FtpClient.GetListing(String path, FtpListOption options)\r\n at System.Net.FtpClient.FtpClient.GetListing(String path)\r\n at Service.Ftp.Workflows.Download.Actions.DownloadFilesAction.Execute(Workflow workflow) in e:\Development\source\Service.Ftp\Workflows\Download\Actions\DownloadFilesAction.cs:line 45\r\n
When this exception occurs, I am using System.Net.FtpClient as follows:
ftpClient.ValidateCertificate += delegate(FtpClient control, FtpSslValidationEventArgs e) { e.Accept = true; };
ftpClient.Host = "host";
ftpClient.Credentials = new NetworkCredential("username", "password");
ftpClient.EncryptionMode = FtpEncryptionMode.Explicit;
ftpClient.DataConnectionEncryption = true;
ftpClient.Connect();
List<FtpListItem> remoteFiles = ftpClient.GetListing("/RemoteFolder/").ToList();
The interesting thing is that the Connect()
call works - it's the GetListing()
that fails.
The above code works perfectly on WIndows 7 and Windows Server 2008 R2, but fails on Windows Server 2012.
If anyone has any ideas, I'd love to hear them.