我想从 Active Directory 获取 OU 列表。
我只有域名。
我如何使用 C# 实现这一目标?
尝试这样的事情:
// connect to "RootDSE" to find default naming context
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();
// bind to default naming context - if you *know* where you want to bind to -
// you can just use that information right away
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);
// set up directory searcher based on default naming context entry
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);
// SearchScope: OneLevel = only immediate subordinates (top-level OUs);
// subtree = all OU's in the whole domain (can take **LONG** time!)
ouSearcher.SearchScope = SearchScope.OneLevel;
// ouSearcher.SearchScope = SearchScope.Subtree;
// define properties to load - here I just get the "OU" attribute, the name of the OU
ouSearcher.PropertiesToLoad.Add("ou");
// define filter - only select organizational units
ouSearcher.Filter = "(objectCategory=organizationalUnit)";
// do search and iterate over results
foreach (SearchResult deResult in ouSearcher.FindAll())
{
string ouName = deResult.Properties["ou"][0].ToString();
}
如果您有域名(例如mycompany.com
),然后是 LDAP 根域通常将被称为dc=mycompany,dc=com
- 这是惯例,但不一定非得如此。这就是为什么我要连接到LDAP://RootDSE
虚拟 LDAP 根,我读出该属性Default Naming Context
这给了我默认的 LDAP 路径。
If you know您想要连接到的位置 - 请随意跳过第一步,只需提供有效的 LDAP 路径(例如LDAP://dc=YourCompany,dc=co,dc=jp
或其他)来创建domainRoot
目录条目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)