I have developped a web service in C# .Net 4.5, which connect to a ftp server (with SSL) and list all the files stored in a directory of the ftp server. The code worked fine last year (beginning of 2019 or end of 2018) but since I tested the code again 3 weeks ago and it doesnt not work anymore. I have tried many things:
-I have changed the target framework from 4.5 to 4.8 (Link of the article)
-Use the fluentFTP nuget package (but I have the same error
The thing is that I can connect to the ftp server with Filezilla and access to the directory without any error (So I guess that it is not a firewall issue) I have checked the logs of ftp exchanges between my computer and the ftp server and the error occurs during the ftp command MLSD -> Opening data channel for directory listing of "directory" (last message from the server -> .Net error: "authentication failed because the remote party has closed the transport stream"
Here is the code:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://urlFtpServer:21/directory");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.EnableSsl = true;
// Sets the user login and password.
request.Credentials = new NetworkCredential("login", "password");
request.KeepAlive = true;
try
{
// Send the request.
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
IEnumerable<string> lstDirectoryFiles = reader.ReadToEnd()
.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
// Use the user criteria to get only the file needed.
if (string.IsNullOrEmpty(in_searchPattern))
return lstDirectoryFiles.ToList();
Regex rgx = new Regex(in_searchPattern);
return lstDirectoryFiles.Where(st => rgx.IsMatch(st)).ToList();
}
}
}
}
catch (Exception ex)
{
//Here is the exception: authentication failed because the remote party has closed the transport stream
}
Please help :)
I forgot to mention that the ftp request method WebRequestMethods.Ftp.MakeDirectory works fine