Search code examples
c#ftpuploadwindows-firewall

C# Uploading a file to remote FTP is forbidden by Windows firewall


I use C# FtpClient library to upload a file. The connection is established after I set custom port 3072, because I set client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;

But when I am going to go through

client.GetFilePermissions("/Test.txt");
client.UploadFile(@"C:\Users\Desktop\Test.txt", "/Test.txt");

it will always shows exception

Unable to read data from the transport connection: An attempt was made to access a socket in a way forbidden by its access permissions.

If I turn Windows Firewall off, file can be uploaded successfully.

I want to know what policy should I set on Windows firewall to allow me touch remote file and upload it.

Current my firewall setting:

  • (Inbound)
    Local port 3072,80,20,21,1023
    Remote port 3072,80,20,21,1023

  • (Outbound)
    Local port 3072,80,20,21,1023
    Remote port 3072,80,20,21,1023

My complete code

FtpClient client = new FtpClient();
client.Host = "xx.xx.xx.xx";
client.Credentials = new NetworkCredential(UserName, Password);
client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;

client.Connect();
if (client.IsConnected)
{
    Console.WriteLine("Connected");
    client.DataConnectionEncryption = true;
    var resutl = client.GetFilePermissions("/Test.txt");
    client.UploadFile(@"C:\Users\Desktop\Test.txt", "/Test.txt");
}
else
{
    Console.WriteLine("No Connetion");
}

Solution

    • Open an Administrator command-prompt. Click Start, click All Programs, click Accessories, right-click Command Prompt, and then click Run as Administrator.

    • Run the following command:

    1.netsh advfirewall firewall add rule name=”FTP Service” action=allow service=ftpsvc protocol=TCP dir=in

    2.netsh advfirewall set global StatefulFTP disable

    https://technet.microsoft.com/en-us/library/dd421710(v=WS.10).aspx