我正在尝试使用LDAP列出域中位于组织单位中的所有用户(
DirectorySearcher class
我连接的域不是当前域,我尝试查看的OU位于一个非常深的路径中,其中一些OU名称在其他地方重复出现,例如:
MyDomain.LOCAL文件
/MyCompany/客户/Contoso/金融网站/金融服务/
我可以用以下代码列出域中的所有用户:
// Build the directory entry
var directoryEntry = new DirectoryEntry(_ldapServer, _domain + "\\" +
_systemUser, _systemPassword);
try
{
// Bind to the native AdsObject to force authentication of the system user.
// It will throw an exception if this is an invalid account
object obj = directoryEntry.NativeObject;
}
catch (Exception ex)
{
throw new Exception("Error authenticating system user. " + ex.Message, ex);
}
// create a directory searcher for that OU
DirectorySearcher users = new DirectorySearcher(directoryEntry);
// set the filter to get just the users
users.Filter = "(&(objectClass=user)(objectCategory=Person))";
// add the attributes you want to grab from the search
users.PropertiesToLoad.Add("givenName");
users.PropertiesToLoad.Add("sn");
users.PropertiesToLoad.Add("mail");
users.PropertiesToLoad.Add("name");
// grab the users and do whatever you need to do with them
var allFound = users.FindAll();
foreach (SearchResult oResult in allFound)
{
// etc
}
但是,我希望获得特定OU下的用户。
我试过以下几句话:
var directoryEntry = new DirectoryEntry(_ldapServer +
"/ou=MyCompany/Clients/Contoso/Financial Site/Financial Services/Users",
_domain + "\\" + _systemUser, _systemPassword);
我得到一个错误:
Error authenticating system user. An operations error occurred.
有没有人知道我是如何在
DirectorySearcher
解决了的!
最后一个路径字符串(以我的示例为例)应如下所示(不带换行符):
LDAP://DomainControllerServer/OU=Users,OU=Financial Services,
OU=Financial Site,OU=Contoso,OU=Clients,OU=MyCompany,
DC=MyDomain,DC=LOCAL
DomainControllerServer = IP address in my case.
-- FQDN: MyDomain.LOCAL - Period-separated into DC={part} list
|-- OU: MyCompany
|-- OU: Clients
|-- OU: Contoso
|-- OU: Financial site
|-- OU: Financial Services
|-- OU: Users
\
),例如以下任何一种:
+ , \ = /
这是一场噩梦,但谢天谢地,它现在起作用了。