0

I'm actualy developping a WindowsForm application, and i'd like my application download a csv file on a SFTP server.

So, when i logged-in with Windows form login, that's work : Something like this

But, i don't want a user can authenticate, and i'd like my application authenticate alone.

So, i found something like this on internet :


using (var client = new SftpClient(host, port, username, password))
{
                
      try
      {
           // Create the new LDAP connection
           LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier("XXX.XXXX", 389);
           LdapConnection ldapConnection = new LdapConnection(ldi);
           Console.WriteLine("LdapConnection is created successfully.");
           ldapConnection.AuthType = AuthType.Basic;
           ldapConnection.SessionOptions.ProtocolVersion = 3;
           ldapConnection.SessionOptions.AutoReconnect = true;
           NetworkCredential nc = new NetworkCredential("USER", "PASSWORD"); //password
           ldapConnection.Bind(nc);
           Console.WriteLine("LdapConnection authentication success");
                    
           DownloadFilesFromSFTP(client, remoteDirectory, _URI_FILE_EXPORTED, false);

                    
           // Disconnect
           //ldapConnection.Dispose();
       }
       catch (LdapException errorLdap)
       {
           Console.WriteLine("\r\nUnable to login:\r\n\t" + errorLdap.Message);
       }
       catch (Exception errorLdap)
       {
           Console.WriteLine("\r\nUnexpected exception occured:\r\n\t" + e.GetType() + ":" + errorLdap.Message);
       }
                
           //DownloadDirectory(client, remoteDirectory, _URI_FILE_EXPORTED, false);
}

foreach (SftpFile file in files)
{
    // If is a file, download it
    if (!file.IsDirectory && !file.IsSymbolicLink && !fileTreated.Contains(file.Name))
    {
          DownloadFile(client, file, destination);
    }
}
private void DownloadFile(SftpClient client, SftpFile file, string directory)
{
     Console.WriteLine("Downloading {0}", file.FullName);

     using (Stream fileStream = File.OpenWrite(Path.Combine(directory, file.Name)))
     {
         client.DownloadFile(file.FullName, fileStream);
         lbl_file_downloading.Text = "Nombre de fichier(s) téléchargé(s) : " + numberFileDownloaded;
                progressBar1.PerformStep();
     }
     numberFileDownloaded++;

     if (numberFileDownloaded > fileQuantity)
     {
         status_downloaded_file.Image = Resources.green_point_2020;
         progressBar1.Value = 0;
     }
     else
     {
         status_downloaded_file.Image = Resources.red_point_2020;
     }
}

My code block at this point :

 using (Stream fileStream = File.OpenWrite(Path.Combine(directory, file.Name)))

as an access refused

As you can see :

    LdapConnection authentication success
Downloading /DIRECTORY/AFZEAF_423446342342143_EZR.csv

Unexpected exception occured:
        System.Windows.Forms.MouseEventArgs:L'accès au chemin d'accès '\\SERVER\test\downloaded_files\AFZEAF_423446342342143_EZR.csv' est refusé.

My user have the correct right, when i loggon, there is no problem ..

If someone as an explaination ! Maybe i've done something bad, (probably ahah).

Thanks in advance !

Dusk
  • 11
  • 2
  • Why are you trying to use LDAP or a login form at all? Windows authentication works without writing *any* code. All applications run under the account of the user that started them. The application *and* .NET already know who the user is. You can get the user with `WindowsIdentity.GetCurrent()`. Access to files, remote servers and databases is done using the user's credentials. You can check the user's group membership directly to determine permissions – Panagiotis Kanavos Mar 16 '21 at 09:27
  • `i found something like this on internet` that's a great way to add vulnerabilities, backdoors or simply introduce bugs to your application. It's never a good way to solve problems. You need to understand what you're doing first. In this case, authentication is provided by the operating system. Authentication and authorization is covered in the docs for all stacks (WinForms, WPF. ASP.NET, Core etc) since the very first .NET release. In fact, you need to know about both to pass even entry-level certifications – Panagiotis Kanavos Mar 16 '21 at 09:32
  • 1
    What does `i'd like my application authenticate alone` mean? SFTP isn't related to LDAP or Windows Authentication. It allows two ways to authenticate - with explicit username/password or through certificates. Either you hard-code or ask the user for the username/password combination, or configure your SFTP client to use certificates and configure the *SFTP Server* to recognize the client's public key – Panagiotis Kanavos Mar 16 '21 at 09:41

0 Answers0