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

发布于 2024-11-24 08:56:26 字数 1154 浏览 5 评论 0原文

我正在尝试获取 FTP 站点的目录列表,但在连接和检索该列表时遇到问题。我认为问题出在 Windows 2008 Server R2 的 Windows 防火墙上。这是代码:

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

我想知道需要打开哪些防火墙设置或端口才能允许此操作发生。我启用了端口 21 和 20 上的所有传入/传出流量,但这不起作用,因此我允许所有端口的所有传入/传出流量并且起作用。然而,像这样开放每个端口并不是一个可行的解决方案:)

I am trying to get a directory listing of an FTP site but am having issues connecting and retrieving the listing. I believe the problem is with the windows firewall for Windows 2008 Server R2. 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. However, opening up every port like that is not a viable solution :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-12-01 08:56:30

我终于弄清楚了。您需要在 Windows 防火墙中围绕 w3p 进程创建自定义入站流量规则。这是一个演练。

在入站流量中创建新规则
选择自定义

选择“此程序路径”

找到 w3wp.exe 服务(通常在 Windows\System32 文件夹中)

单击自定义

选择“仅应用于服务”

将协议类型更改为 TCP

将本地和远程端口设置为“所有端口”(您可以尝试将其范围缩小到仅端口 21,但这并不总是有效,尤其是异步上传)

选择任何 IP 地址作为本地 IP 地址(如果您知道本地 IP 地址范围,则选择一个范围)

选择“这些 IP 地址”对于远程 IP 地址并单击添加。
输入您要上传的 FTP 站点的 IP 地址

添加 FTP IP 地址后单击“确定”,然后单击“下一步”

选择允许连接

将规则应用于域、专用和公共

名称为“允许来自以下来源的 TCP w3wp.exe 传入流量”端口 21”

单击完成

I finally figure it out. You need to create a custom inbound traffic rule in Windows Firewall around the w3p process. Here is a walkthrough.

Create a new Rule in Inbound traffic
Select Custom

Select “This Program Path”

Find the w3wp.exe service (usually in Windows\System32 folder)

Click Customize

Select “Apply to services only”

Change the Protocol Type to 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)

Select any IP address for local IP addresses (or a range if you know the local IP address range)

Select “These IP addresses” for remote IP addresses and click Add.
Enter the IP address of the FTP site you will be uploading too

Click OK once you have added the FTP IP address, then click Next

Select Allow the Connection

Apply rule to Domain, Private and Public

Name is “Allow incoming TCP w3wp.exe traffic from port 21”

Click finish

如何视而不见 2024-12-01 08:56:30

我认为这并不真正适合 StackOverflow,因为这个问题更与 Windows Server 2008 R2 的安全配置有关。但是,总的来说,我测试了您的代码,它运行得很好,因此您需要为服务器上所需的 FTP 端口的 FTP 访问设置 Windows 防火墙例外。否则,Windows 防火墙确实会阻止来自客户端的传入连接。如果打开防火墙例外不能解决问题,那么您将需要调查服务器上的其他安全配置问题。

I don't think this is really suited to StackOverflow, because this question moreso pertains to security configuration for 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. Otherwise, Windows Firewall will indeed block your incoming connections from your client. If opening a Firewall Exception does not fix the issue then you will want to investigate other security configuration concerns on your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文