Search code examples
c#ftpwinscpftp-clientwinscp-net

WinSCP .NET library FTP transfer fails with "IP addresses of control and data connection do not match"


I have implemented FTP code with use of WinSCP .NET assembly and hosted it on an Azure AppService.

It works locally and on Azure.

But in very few random times, when hosted on Azure, it throws the following error:

Error transferring file 'D:\local\Temp\test_settings.txt'. Server sent passive reply with unroutable address 10.YYY.YYY.YYY, using host address instead. Copying files to remote side failed. Rejected data connection for transfer of "/test_settings.txt", IP addresses of control and data connection do not match

Since the IP starts with 10. does that mean that it's local in the FTP server's network? Can I do something to improve the implementation?

Do you think that the solution will have a problem when used concurrently by multiple requests?

My code is a copy of the Simple C# example with the following settings:

SessionOptions sessionOptions = new SessionOptions
{
  Protocol = Protocol.Ftp,
  UserName = username,
  Password = password,
  GiveUpSecurityAndAcceptAnyTlsHostCertificate = true,
  FtpSecure = FtpSecure.Explicit,
  HostName = 'ftp.domain.com'
};

Solution

  • IP addresses of control and data connection do not match

    That's a message from FileZilla FTP server. It's a security measure. It might indicate that external IP address of your app service instance changed mid transfer. Or of course, it might indicate that you connection was hijacked (that's what the server tries to detect).

    It has nothing to do with WinSCP.

    I do not know if the IP address of the Azure app service can be fixed somehow. If not, all you can do is to reconnect and retry the transfer. I believe you would have the same problem with any FTP client. Maybe with IPv6 connection, the problem would not happen. But I'm not sure, it's just a wild guess. Though you cannot force IPv6 with WinSCP (only by disabling IPv4 altogether, but I do not know if that's even possible with the app service).