c# - 使用 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é.
我的用户有正确的权利,当我登录时,没有问题..
如果有人作为解释!也许我做了坏事,(可能啊)。
提前致谢 !
解决方案
推荐阅读
- reactjs - 如何将 React 钩子用于函数并将函数作为道具传递以创建通用模式?
- java - 在单链表的java实现中,为什么不打印链表的最后一个元素?
- javascript - 反应渲染中的嵌套返回html
- python - 如何在 python 中使用 ElasticSearch-dsl 自定义同义词标记过滤器?
- bash - CentOS 中的 PATH 定义在哪里?
- json - 重定向restfull api codeigniter
- reactjs - Strapi 和 Sendgrid 的 HTTP POST 400 错误
- android - 如何在 Android 7 和 Nougat 上接听来电?
- android - 反应原生
在 Android 上不呈现图像 - r - "按州缩写比较变量