简体   繁体   English

C# 中的防火墙和 FTP 目录列表

[英]Firewall and FTP Directory Listing in C#

I am trying to get a directory listing of an FTP site but am having issues connecting and retrieving the listing.我正在尝试获取 FTP 站点的目录列表,但在连接和检索列表时遇到问题。 I believe the problem is with the windows firewall for Windows 2008 Server R2.我相信问题出在 Windows 2008 Server R2 的 windows 防火墙上。 Here is the code:这是代码:

        try
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1");
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            request.UsePassive = false;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("user", "pass");
            request.Proxy = HttpWebRequest.DefaultWebProxy;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            Label1.Text = reader.ReadToEnd();

            reader.Close();
            response.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

I am wondering what firewall settings or ports need to be opened to allow this action to happen.我想知道需要打开哪些防火墙设置或端口才能允许此操作发生。 I enabled all incoming/outgoing traffic on ports 21 and 20 and that didn't work so I allowed all traffic incoming/outgoing for all ports and that worked.我在端口 21 和 20 上启用了所有传入/传出流量,但这不起作用,因此我允许所有端口的所有传入/传出流量,并且有效。 However, opening up every port like that is not a viable solution:)但是,像这样开放每个端口并不是一个可行的解决方案:)

I don't think this is really suited to StackOverflow, because this question moreso pertains to security configuration for Windows Server 2008 R2.我不认为这真的适合 StackOverflow,因为这个问题更涉及 Windows Server 2008 R2 的安全配置。 But, in light, I tested your code, and it works just fine, so you need to make a Windows Firewall exception for FTP access on the server for the required FTP ports.但是,在光,我测试了你的代码,它工作得很好,所以你需要为所需的 ZC728A49363C9A93A43A7E7F23AZ2B5A54 端口在服务器上的 FTP 访问创建一个 Windows 防火墙例外。 Otherwise, Windows Firewall will indeed block your incoming connections from your client.否则,Windows 防火墙确实会阻止来自客户端的传入连接。 If opening a Firewall Exception does not fix the issue then you will want to investigate other security configuration concerns on your server.如果打开防火墙例外不能解决问题,那么您将需要调查服务器上的其他安全配置问题。

I finally figure it out.我终于弄明白了。 You need to create a custom inbound traffic rule in Windows Firewall around the w3p process.您需要在 Windows 防火墙中围绕 w3p 进程创建自定义入站流量规则。 Here is a walkthrough.这是一个演练。

Create a new Rule in Inbound traffic Select Custom在入站流量中创建新规则 Select 自定义

Select “This Program Path” Select “此程序路径”

Find the w3wp.exe service (usually in Windows\System32 folder)找到 w3wp.exe 服务(通常在 Windows\System32 文件夹中)

Click Customize单击自定义

Select “Apply to services only” Select “仅适用于服务”

Change the Protocol Type to TCP将协议类型更改为 TCP

Set local and remote ports to “All Ports” (you can try to narrow it down to only port 21, but that doesn't always work, especially with asynchronous uploads)将本地和远程端口设置为“所有端口”(您可以尝试将其缩小到仅端口 21,但这并不总是有效,尤其是异步上传)

Select any IP address for local IP addresses (or a range if you know the local IP address range) Select 本地 IP 地址的任何 IP 地址(或者如果您知道本地 ZA12A3079E14CEDB8A90B21AZ 地址范围,则为范围)5BA89A

Select “These IP addresses” for remote IP addresses and click Add.远程 IP 地址的 Select “这些 IP 地址”,然后单击添加。 Enter the IP address of the FTP site you will be uploading too输入您将要上传的 FTP 站点的 IP 地址

Click OK once you have added the FTP IP address, then click Next添加 FTP IP 地址后单击确定,然后单击下一步

Select Allow the Connection Select 允许连接

Apply rule to Domain, Private and Public将规则应用于域、私有和公共

Name is “Allow incoming TCP w3wp.exe traffic from port 21”名称为“允许来自端口 21 的传入 TCP w3wp.exe 流量”

Click finish点击完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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