首页 > 解决方案 > 使用 WindowsForm 自动通过 LDAP 连接到 Windows Server

问题描述

我实际上正在开发一个 WindowsForm 应用程序,我希望我的应用程序在 SFTP 服务器上下载一个 csv 文件。

因此,当我使用 Windows 表单登录登录时,这是可行的:类似这样的东西

但是,我不希望用户可以进行身份​​验证,我希望我的应用程序单独进行身份验证。

所以,我在网上找到了这样的东西:


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;
     }
}

此时我的代码块:

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

作为访问被拒绝

如你看到的 :

    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é.

我的用户有正确的权利,当我登录时,没有问题..

如果有人作为解释!也许我做了坏事,(可能啊)。

提前致谢 !

标签: c#windowsldapconnection

解决方案


推荐阅读