LDAP connection to a Windows Server with WindowsForm automatically

225 Views Asked by At

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 !

0

There are 0 best solutions below