Reputation: 6514
I keep receiving the error "The remote server returned an error: (530) Not logged in." when FtpWebRequest.GetResponse() is called. I suspect it's because I've misconfigured the FTP site for Basic authentication, but I'm not sure. I've read other posts, but the solutions seem to revolve around the incorrect credentials being specified in the NetworkCredential object. I'd like to be able to pass credentials to the ftp site over SSL. I've isolated the problem to a test project. Here it is, mostly copied from the msdn https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.enablessl(v=vs.110).aspx:
public static void Main(string[] args)
{
// Only use this to get around issues with self-signed certificates...
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; };
ListFilesOnServerSsl(new Uri("ftp://127.0.0.1:21"));
Console.WriteLine("Press any key...");
Console.ReadKey();
}
public static bool ListFilesOnServerSsl(Uri serverUri)
{
// The serverUri should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = false;
request.EnableSsl = true;
// Need to use credentials to log in...
request.Credentials = new NetworkCredential("TestFtpUser", "IDontKnow");
// Get the ServicePoint object used for this request, and limit it to one connection.
// In a real-world application you might use the default number of connections (2),
// or select a value that works best for your application.
ServicePoint sp = request.ServicePoint;
Console.WriteLine("ServicePoint connections = {0}.", sp.ConnectionLimit);
sp.ConnectionLimit = 1;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("The content length is {0}", response.ContentLength);
// The following streams are used to read the data returned from the server.
Stream responseStream = null;
StreamReader readStream = null;
try
{
responseStream = response.GetResponseStream();
readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
if (readStream != null)
{
// Display the data received from the server.
Console.WriteLine(readStream.ReadToEnd());
}
Console.WriteLine("List status: {0}", response.StatusDescription);
}
finally
{
if (readStream != null)
{
readStream.Close();
}
if (response != null)
{
response.Close();
}
}
Console.WriteLine("Banner message: {0}",
response.BannerMessage);
Console.WriteLine("Welcome message: {0}",
response.WelcomeMessage);
Console.WriteLine("Exit message: {0}",
response.ExitMessage);
return true;
}
What follows are the screenshots for creating the ftp site on my localhost in IIS:
I noticed that IIS didn't ask me for a password when Basic authentication was set up. I don't know why this is. Should I not be using Basic authentication for the FTP site?
Many thanks in advance!
Upvotes: 3
Views: 17189
Reputation: 21
I was having problems in general connecting to a FTP server with C# code and getting an error like FTPWebRequest 530 Error: Not Logged in.
Turns out, the ftp site I was trying to connect to uses FTP with TLS as noted by this when I could connect to it with Filezilla (proving my credentials username and pw were valid):
Status: Connection established, waiting for welcome message... Status: Initializing TLS... Status: Verifying certificate... Status: TLS connection established. Status: Logged in
I just had to add to my FtpWebRequest to have request.EnableSSL = true; and then it worked and no longer received the 530 error... example:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.EnableSsl = true;
Hope that helps some people out there!
TJ
Upvotes: 2
Reputation: 6514
It turns out that I hadn't configured a local user account on the machine to match the "specified user" when configuring the ftp site in IIS. After going to "Control Panel" => "User Accounts" => "User Accounts" => "Manage User Accounts" => "Manage User Accounts" => Add, and adding the user to my local machine, the message "(530) Not Logged In" has gone away.
Upvotes: 1