San*_*onu 5 c# active-directory
我想从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();
}
Run Code Online (Sandbox Code Playgroud)
如果你有一个域名(例如mycompany.com),那么通常会调用LDAP根域dc=mycompany,dc=com- 这是一个约定,但它不一定是那样.这就是我连接到LDAP://RootDSE虚拟LDAP根目录的原因,我读出了属性Default Naming Context,它给了我默认的LDAP路径.
如果您知道要连接的位置 - 请随意跳过第一步,只提供有效的LDAP路径(例如LDAP://dc=YourCompany,dc=co,dc=jp或其他)来创建domainRoot目录条目.
| 归档时间: |
|
| 查看次数: |
12429 次 |
| 最近记录: |