我的目的是在控制台 C# 应用程序中连接到 Active Directory(在虚拟机 (Win SRV 2008R2) 上运行)并记下域中的所有用户名。由于我是 AD 新手,所以我在设置连接时遇到了困难。
现在第一件事是第一;
根域名 = frt.local
IP:192.168.x.x
用户名:admin
通过:yyyy
我编写了下面的代码来设置连接,但出现错误。请告诉我我错过的一点。
DirectoryEntry entry = new DirectoryEntry();
entry.Path = "LDAP://192.168.x.x/dc=frt.local";
entry.Username = @"frt.local\admin";
entry.Password = "yyyy";
在指出我错过的内容之后,非常欢迎将用户名写到控制台的任何帮助。
亲切的问候
最佳答案
Nesim 的回答很好——一开始。但我真的不认为使用它有任何意义或必要
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
行-PrincipalSearcher 的结果已经是一个 UserPrincpial,您可以像这样更轻松地访问其属性:
using (var searcher = new PrincipalSearcher(new UserPrincipal(ctx)))
{
foreach (var result in searcher.FindAll())
{
UserPrincipal foundUser = result as UserPrincipal;
if(foundUser != null)
{
Console.WriteLine("First Name: {0}", foundUser.GivenName);
Console.WriteLine("Last Name : {0}", foundUser.Surname);
Console.WriteLine("SAM account name; {0}", foundUser.SamAccountName);
Console.WriteLine("User principal name: {0}", foundUser.UserPrincipalName);
Console.WriteLine();
}
}
}
UserPrincipal
已经很好地将最常用的属性公开为对象本身的属性 - 不需要使用 DirectoryEntry
编写相当困惑的代码...
关于c# - 事件目录连接 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12943308/