How to login FTP with empty password in C#?

3.2k views Asked by At

For some reason I need to login FTP server with empty/blank/no password.
That should be pretty straightforward. Here is code:

try
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/folder/file.csv");

    request.Credentials = new NetworkCredential("VTIE", "");
    request.Method = WebRequestMethods.Ftp.DownloadFile;

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

    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    StreamWriter writer = new StreamWriter(@"D:\\text.txt");
    writer.Write(reader.ReadToEnd().ToString());

    writer.Close();
    response.Close();

}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

However, this results in an error message:

The remote server returned an error:(500) syntax error, command unrecognized.

Network Tracing Log:

System.Net Information: 0 : [4864] FtpWebRequest#1013293::.ctor(ftp://xxx.xxx.xxx.xxx/folder/file.csv)
System.Net Information: 0 : [4864] FtpWebRequest#1013293::GetResponse(Method=RETR.)
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Created connection from YYY.YYY.YYY.YYY:57358 to xxx.xxx.xxx.xxx:21.
System.Net Information: 0 : [4864] Associating FtpWebRequest#1013293 with   FtpControlStream#45598209
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received  response [220 VT-E1 FTP server ready.]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Sending command [USER UserName]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received response [331 User name okay, need password.]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Sending command [PASS]
System.Net Information: 0 : [4864] FtpControlStream#45598209 - Received response [500 Syntax error, command unrecognized.]

So, after USER command there's a PASS command, but it is rejected with "Syntax Error". What is going on here? How can I use FtpWebRequest class with empty password?

I use FileZilla FTP client to connect. Here is log:

Status: Connecting to xxx.xxx.xxx.xxx:21...
Status: Connection established, waiting for welcome message...
Response:   220 VT-E1 FTP server ready.
Command:    USER VTIE
Response:   331 User name okay, need password.
Command:    PASS 
Response:   230 User logged in, proceed.
Command:    SYST
Response:   202 Command not implemented.
Command:    FEAT
Response:   500 Syntax error, command unrecognized.
Status: Server does not support non-ASCII characters.
Status: Connected
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "/" is current directory.
Command:    TYPE I
Response:   200 TYPE command successful.
Command:    PASV
Response:   227 Entering Passive Mode (xxx,xxx,xxx,xxx,18,46).
Command:    LIST
Response:   150 Opening data connection for (LIST) (xxx.xxx.xxx.xxx,4654).
Response:   226 Transfer complete.
Status: Directory listing of "/" successful
1

There are 1 answers

1
Martin Prikryl On

The FtpWebRequest always issues both USER and PASS FTP commands. So if you do not specify any password, the FtpWebRequest sends a PASS command without an argument. Your server does not like that. Although you claim that you do not need to use a password to login.

There several options:

  • Either your FTP server just requires a syntactically valid PASS command. Possibly it does not consider PASS syntactically correct. Note that FileZilla sends PASS<space>, not just PASS. You cannot make FtpWebRequest send PASS<space> directly. So maybe the FTP server does not care what password your use. Try to use a random string for password. When you do not specify any username, the FtpWebRequest defaults to username "anonymous" and password "anonymous@". Try that.
  • Or your password is really an empty string (and it really gets validated). And the FTP server requires the space (argument separator) in PASS command. As I wrote above, FtpWebRequest cannot do that. But you can try using a password "\r\n". This way the FtpWebRequest sends PASS \r\n\r\n\. I.e. PASS<space> followed by an empty line. Hopefully the FTP server ignores the empty line. I've tested with with FileZilla FTP server and it does ignore the empty line. But this can be server-specific.