Your code looks like it is probably fine—more than likely you can resolve this by modifying the IIS FTP Server settings.
First, some ideas to get to a base known configuration.
- Open the IIS Manager tool.
- Let's ensure you still have
IUSR
as the user that IIS uses for Anonymous Authentication in its internal security scheme. Click on the top node (should be your computer name) in the left sidebar, then click on Authentication
under the IIS
section in the main content area. Ensure Anonymous Authentication
is enabled. Click on this item, then click Edit
in the right sidebar. Make sure this is already set to Specific User
with a value of IUSR
. As long as this is the case, we don't need to get into dealing with the IUSR
password etc.
- Click on your FTP site in the left sidebar.
- In the right sidebar, click
Basic Settings
. Make a note of your directory where the site points-e.g. C:\inetpub\ftproot
, and ensure this matches up with the expected location of the test file your code is trying to download.
- In the right sidebar, click on
Bindings
. Since your code doesn't mention a specific port, ensure port 21
is set here. As far as the IP Address
, leaving it set to All Unassigned
should be fine, or you can set to `127.0.0.1'.
- In the main window area, let's go through the settings.
FTP Authentication
: Anonymous Authentication
should be set to Enabled
. Basic Authentication
should be set to Disabled
. Hint: use the <alt>
+<left>
keyboard shortcut to return to the main list of modules.
FTP Authorization
: Add an Allow Rule
as follows: Choose Specified Users
, and set to IUSR
. Grant Read
permission.
FTP IP Address and Domain Restrictions
: Remove any configured rules that show here if you see any. In the right sidebar click Edit Feature Settings
, then make sure it is set to Allow
under Access for Unspecified Clients
. You can leave Enable Domain Retrictions
unchecked.
FTP SSL Settings
: Check Allow SSL Connections
. (Enabling Require SSL connections
is not what you want here.)
- Ok, now in the left sidebar, right-click on your FTP site and select
Edit Permissions
which will pop open the properties window. Go to the Security
tab, then you will make some ACL changes if needed. Most importantly, the IUSR
account needs at least the following: Read & Execute
, List folder contents
, Read
. This will let you your code download files anonymously. You may need to add Modify
and Write
for uploading capabilities. To make the change, you may need to disable inheritance from the parent directory—if so, I would recommend copying the existing permissions rather than starting with a blank slate. You may want to use the Replace all child object permissions...
option if you have made any changes here, to ensure everything from the root down now has the same access control entries.
- Open an admin command prompt and type
iisreset
Ok, now that IIS has been configured, let's look at the code. After placing a small text file called test.txt
in the root of the FTP site, I got the below code, adapted from your sample, to work on my local machine:
string ftpHost = "ftp://localhost/test.txt";
FtpWebRequest request = (FtpWebRequest) WebRequest.Create(ftpHost);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//anonymous logon
request.Credentials = new NetworkCredential("anonymous", "[email protected]");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
Console.ReadLine();
[Disclaimer: These instructions are to configure a local FTP server working on a development machine, to allow the .NET FtpClient class to connect to it anonymously. Do not consider these good instructions for use on a Production environment.]