Kelly
Kelly

Reputation: 1005

Accessing localhost FTP server via .Net

I have an MVC application and am trying to access my localhost ftp server I set up in IIS- eventually it'll be accessing some other ftp server but I just want to try it out locally. I wrote a c# function using FtpWebRequest but when I call GetResponse() it returns the error: "The remote server returned an error: (530) Not logged in." Not sure exactly how to log in as anonymous or non-anonymous user. Is the URL string formatted correctly if I have an FTP site in IIS called "MyFTPSite" and Anonymous Authentication is enabled? What should networkcredential be? The Files directory is a child of ftproot- do I need to add a virtual directory for the Files folder? Right now the physical path of the ftp site is set to C:\inetpub\ftproot. (In below code, localhost purposely spelled wrong b/c the site didn't want me to use a localhost url).

 public override bool DownloadFile()
    {
        string fileName = System.IO.Path.GetFileName(this.UrlString);

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(this.UrlString);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        //anonymous logon
        request.Credentials = new NetworkCredential("anonymous", "<my email>");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        response.Close();
        return true;

    }

FTPDownload download = new FTPDownload(@"ftp://lclhost/MyFTPSite/Files/w4.pdf");
download.DownloadFile();

Upvotes: 0

Views: 7265

Answers (2)

helpME1986
helpME1986

Reputation: 1013

If you set anonymous logon on IIS do not set any Network Credentials. Comment it out:

  //anonymous logon
  //  request.Credentials = new NetworkCredential("anonymous", "<my email>");

Upvotes: 1

Thomas W
Thomas W

Reputation: 116

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.

  1. Open the IIS Manager tool.
  2. 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.
  3. Click on your FTP site in the left sidebar.
  4. 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.
  5. 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'.
  6. 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.)
  7. 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.
  8. 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.]

Upvotes: 2

Related Questions