iT邦幫忙

0

透過C# FtpClient 上傳檔案 被 Windows 7 防火牆擋住

kunyao 2017-06-25 06:15:2810744 瀏覽

透過C# FtpClient 函式庫上傳CSV檔案到遠端FTP空間

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 result = client.GetFilePermissions("/Test.txt");
   client.UploadFile(@"C:\Users\Desktop\Test.txt", "/Test.txt");
}
else
{
   Console.WriteLine("No Connetion");
}

但當程式到了以下程式碼
client.GetFilePermissions("/Test.txt");

會出現以下錯誤

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

但當我防火牆全關時候,檔案是可以順利上傳遠端FTP的

目前防火牆設定例外
(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

因此想問的是防火牆的inbound 或outbound要怎麼設定才能讓上傳檔案到遠端的FTP

kunyao iT邦新手 5 級 ‧ 2017-07-17 15:36:15 檢舉
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
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2017-06-25 13:10:56

打開 port 20,21試試看

或者應該說「開放 port 20,21」(不擋)

kunyao iT邦新手 5 級 ‧ 2017-06-25 17:13:38 檢舉

嗯! 我目前Inbound和Outbound都有開20, 21
不曉得為什麼還是不行

1
Ray
iT邦大神 1 級 ‧ 2017-06-26 02:06:02

請先確認你用的是 FTP Active 還是 FTP Passive 傳輸?

http://blog.miniasp.com/post/2008/06/29/FTP-Protocol-Definitive-Explanation.aspx

這兩種模式開 Port 的方法是不一樣的....

看更多先前的回應...收起先前的回應...
kunyao iT邦新手 5 級 ‧ 2017-06-29 15:06:55 檢舉

請問~ 確認我用的是 是指 我的程式使用的模式嗎?
或是我IIS所在的作業系統所使用模式呢?

Ray iT邦大神 1 級 ‧ 2017-06-29 18:20:37 檢舉

兩邊都要配合...

Server 設定成 Passvie, Client 用 Active 就打不開
Server 設定成 Active, Client 用 Passive 也打不開

kunyao iT邦新手 5 級 ‧ 2017-07-05 23:27:20 檢舉

對方使用是Passive
而我設FtpWebRequest.isPassive = true
也是連不上...

Ray iT邦大神 1 級 ‧ 2017-07-06 11:37:57 檢舉

您可以參考這邊比對, 對方是否真的開啟了 Passive:
http://blog.xuite.net/ganpin917/home/52930403-%5B%E8%BD%89%E8%BC%89%5DActive+FTP+%E5%8F%8A+Passive+FTP

此外, 你的防火牆必須能夠支援 FTP Passive 的自動反向連線, 否則當對方 Server 要連回來你這邊的時候, 如果防火牆不知道這是 FTP 的 data port, 他就不會自動幫你 forward.

或者是你開啟這樣的 local port mapping: 1024~65535 inbound, 雖然很危險, 但至少這樣可以確保反向一定可以連得回來

kunyao iT邦新手 5 級 ‧ 2017-07-06 15:43:49 檢舉

想問一下 因為目前我測試環境是Win10
而遠端當伺服器的是Win7
如果我是用Win server會不會就沒有防火牆擋FTP的問題?

我要發表回答

立即登入回答