FtpWebRequest:远程服务器返回错误:(530)未登录

时间:2017-01-27 14:01:58

标签: c# .net ssl iis ftp

我一直收到错误"远程服务器返回错误:(530)未登录。"当调用FtpWebRequest.GetResponse()时。我怀疑它是因为我错误配置了FTP站点进行基本身份验证,但我不确定。我已阅读其他帖子,但解决方案似乎围绕NetworkCredential对象中指定的错误凭据。我希望能够通过SSL将凭据传递到ftp站点。我已将问题隔离到测试项目中。这里主要是从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;
}

以下是在IIS中我的localhost上创建ftp站点的屏幕截图:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我注意到IIS在设置基本身份验证时没有要求我输入密码。我不知道为什么会这样。我不应该为FTP站点使用基本身份验证吗?

非常感谢提前!

2 个答案:

答案 0 :(得分:1)

事实证明,在IIS中配置ftp站点时,我没有在计算机上配置本地用户帐户以匹配“指定用户”。转到“控制面板”=> “用户帐户”=> “用户帐户”=> “管理用户帐户”=> “管理用户帐户”=>添加并将用户添加到我的本地计算机,消息“(530)未登录”已消失。

答案 1 :(得分:0)

我通常在使用 C# 代码连接到 FTP 服务器时遇到问题,并收到类似 FTPWebRequest 530 错误:未登录的错误。

事实证明,当我可以使用 Filezilla 连接到它时(证明我的凭据用户名和密码有效),我尝试连接的 ftp 站点使用带有 TLS 的 FTP,如本文所述:

状态:连接已建立,正在等待欢迎信息... 状态:正在初始化 TLS... 状态:正在验证证书... 状态:TLS 连接已建立。 状态:已登录

我只需要在我的 FtpWebRequest 中添加 request.EnableSSL = true;然后它工作了,不再收到 530 错误......例如:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.EnableSsl = true;

希望能帮助到一些人!

TJ