简体   繁体   中英

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");
}

If you indeed block all outbounds/inbound ports, except those listed, FTP can hardly work.

FTP protocol uses a separate transfer connection port range, either outbound (recommended passive mode) or inbound (active mode).

To setup the passive mode, you have to find out, what port range does the FTP server uses, and enabled that in the firewall.

For details, see my article on network setup for FTP protocol .

  • 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

Is it a passive FTP server? If so you may need to open the range of ports like explained here:

https://technet.microsoft.com/en-us/library/083f7757-ad9f-421a-9cde-7a053f3de9a6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM