0

I problem with entering username/password to connect ftp server with credentials :

request.Credentials = new NetworkCredential(UserName, Password);

but when try to connect ftp server with username/password embed in address problem is solved :

ftp://usernamae:password@192.168.1.1:21

how can use this format in my code to connect ftp server :

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
    }
}

for example :

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://username:password@ftp.example.com/remote/path/file.zip");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
    }
}
Tavakkoli
  • 97
  • 11
  • Show us a [`FtpWebRequest` log file](https://stackoverflow.com/q/9664650/850848) for both variants – Martin Prikryl Dec 02 '17 at 08:49
  • @jonathana How could that help? OP needs to use specific credentials, not default credentials. – Martin Prikryl Dec 02 '17 at 08:49
  • @MartinPrikryl, maybe you are right, the comment was deleted. but after reading the question again i think the problem is not the credentials: "but when try to connect ftp server with username/password embed in address problem is solved" – Jonathan Applebaum Dec 02 '17 at 08:53
  • I don't think you can. FTP has been around since the earlier 1970's. Network credentials has been around for less than 10 years. So entering username and password is a protocol that has been around for 45 years and you cannot not change it to a newer protocol. – jdweng Dec 02 '17 at 11:06
  • @jdweng What do you mean by *"Network credentials has been around for less than 10 years"* - Quoting MSDN for [`NetworkCredential`](https://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx): *"Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication."* - You can use `NetworkCredential` for simple FTP authentication. Basicaly both codes in OP are nearly equivalent and normally both work. – Martin Prikryl Dec 02 '17 at 17:50
  • It depends on what the server is programmed to do. Network credentials has been around since Net Framework 1.1. So maybe 15 years instead of 10 , but a lot less than 45. – jdweng Dec 02 '17 at 18:01
  • @jdweng OK, but FTP is not aware of .NET. The `NetworkCredential` is just an API to provide the credentials to FTP client implementation in .NET. The server does not care about `NetworkCredential`. – Martin Prikryl Dec 02 '17 at 18:28
  • Make up your mind. You said "Basicaly both codes in OP are nearly equivalent and normally both work". Now you say network credentials does not work at server. – jdweng Dec 02 '17 at 18:54
  • @Martin Prikryl As I said, if you read it, I was referring to a scenario where a Ftp: (not Ftps:) server was rejecting a request using NetworkCredentials if SSL was active. But I have tested it, and I can't reproduce that behaviour. Maybe it's how the framework worked then (fw 3.5) and how works now (fw 4.7.1). By now, the OP code authenticates in both cases. So I deleted the answer. Maybe his problem is just a typo. – Jimi Dec 02 '17 at 19:25
  • @jdweng There's no contradiction. `FtpWebRequest` just need a username and password. It does not matter, if is gets them from URL or `ICredentials` (implemented by `NetworkCredential`). The FTP server never tells a difference. Because `ICredentials` is not its API, it's an API of the `FtpWebRequest`. For FTP server, the API is `USER` and `PASS` commands. – Martin Prikryl Dec 02 '17 at 19:49
  • @Tavakkoli Do you have any special characters in the password or username? – Martin Prikryl Dec 02 '17 at 19:50
  • @MartinPrikryl no ! – Tavakkoli Dec 03 '17 at 09:14
  • @Tavakkoli So it's only letters and digits? If that's correct, show us logs finally, as I have asked you already yesterday. – Martin Prikryl Dec 03 '17 at 09:20
  • @MartinPrikryl the log is :drwxrwxrwx 1 0 0 4096 Nov 20 2016 usb1_1 drwxr-xr-x 3 0 0 0 Jan 01 1970 . drwxrwxr-x 14 609 609 212 Jul 25 2016 .. – Tavakkoli Dec 05 '17 at 11:54
  • That's not `FtpWebRequest` log. [`FtpWebRequest` log is an XML file](https://stackoverflow.com/q/9664650/850848) + Once you get the real log, append it to your question, do not post it in comments. – Martin Prikryl Dec 05 '17 at 12:00

0 Answers0