I have run into an issue when working with FtpWebRequest
class in C#.
When I try to upload a file to an FTP server using correct credentials for the first time and then wrong credentials (user name the same but wrong password) for the second time, there is no exception thrown and the file is still UPLOADED to the FTP server. Please consider the following code:
using System;
using System.Net;
internal class Program
{
private static void Main(string[] args)
{
var uri = new Uri("ftp://ftp.dlptest.com/TestFile.txt");
var method = WebRequestMethods.Ftp.UploadFile;
//Here I'm uploading the test file using correct credentials
var correctCredentials =
new NetworkCredential("dlpuser@dlptest.com", "fwRhzAnR1vgig8s");
DoFtpRequest(uri, correctCredentials, method);
//Here I'm uploading the test file using wrong credentials.
//I expect some exception to be thrown and the file not being
//uploaded to the server, neither is the case.
var wrongCredentials =
new NetworkCredential("dlpuser@dlptest.com", "WRONG_PASWORD");
DoFtpRequest(uri, wrongCredentials, method);
}
public static FtpWebResponse DoFtpRequest(
Uri uri, NetworkCredential credentials, string method)
{
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
return (FtpWebResponse)request.GetResponse();
}
}
Here I am using a public ftp server ftp://ftp.dlptest.com/
that I found here https://dlptest.com/ftp-test/ which you can use to test this code.
As you can see first I try to upload a file with correct credentials and then with wrong credentials (using the same user name but changing the password). But the file is still uploaded to the server. If I try to use wrong credentials first, the exception is thrown, and everything works as expected.
Do you know what's going on? Is this a bug of the framework? What are my options to deal with this issue, because it causes problems with the program I'm working on now?