简体   繁体   中英

Connecting to same Server with different usernames

I am using the following method to list the contents of a folder:

private void TestFtp()
{
    try
    {
        // List all of the files from FTP
        FtpWebRequest ftprequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://m3database/recover/"));

        ftprequest.Credentials = new NetworkCredential("myusername1", "********");

        ftprequest.Method = WebRequestMethods.Ftp.ListDirectory;
        ftprequest.UsePassive = false;
        ftprequest.Proxy = null;            

        using (var resp = ftprequest.GetResponse())
        {
            StreamReader reader = new StreamReader(resp.GetResponseStream());

            MessageBox.Show(reader.ReadToEnd());
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

I'm connecting to a single Linux server via FTP. I'm using the credentials for myusername1 and myusername2 to connect.

I am able to list the directory contents when using this function if I am using myusername1 credentials, however if I use myusername2 it comes up with the following error:

The remote server returned an error: (503) Bad sequence of commands.

Things that I have tried:

  1. I have tried setting ftprequest.KeepAlive = false .
  2. I have tried using all permutations of UsePassive, Proxy etc.
  3. I am able to connect to both usernames using FileZilla WITH secure FTP enabled and they both work. Without FTP enabled, neither will connect.

It is peculiar that my code will connect using myusername1 without SSL enabled within my code.

In summary:

  • myusername1 and myusername2 will not connect on FileZilla without secure FTP enabled.
  • myusername1 works within my C# method without ftprequest.EnableSsl enabled.
  • myusername2 will not work within my C# method regardless of whether ftprequest.EnableSsl is enabled.
  • The Exception is being triggered on the line of my using() statement.

Something else I just noticed, when I hover over my ftprequest , it shows a NotSupportedException, but this appears regardless of which set of credentials I use

If I hover over my initial ftprequest when using myusername2 , it shows an Exception thrown before my using() :

在此输入图像描述

This might be a shot in the dark, but have you tried using a CredentialCache?

        System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user", "password");
        System.Net.CredentialCache cc = new System.Net.CredentialCache();
        cc.Add(new Uri("ftp://m3database"), "Basic", nc);
        System.Net.FtpWebRequest ftprequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create("ftp://m3database/recover/");
        ftprequest.Credentials = cc;
        ftprequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory;

         using (var resp = ftprequest.GetResponse())
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());

                MessageBox.Show(reader.ReadToEnd());
            }
        }

The credentialcache also gives you a range of authenticationtype options ("NTLM", "Digest", "Kerberos", "Negotiate"... ).

More here --> http://msdn.microsoft.com/en-us/library/59x2s2s6.aspx

I'm connecting to a single Linux server via FTP.

CHMOD the perms on the FTP server/folder for the 2nd User, they should be the same as the 1st User.

CHMOD 777 will allow access required..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM