如何使用c#使用DomainName获取AD中OU名称的列表? [英] How to get list of OU name in AD using DomainName using c#?

查看:516
本文介绍了如何使用c#使用DomainName获取AD中OU名称的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Active Directory中获取OU列表。



我只有域名。



解决方案

尝试如下操作:

  //连接到 RootDSE以查找默认命名上下文。
DirectoryEntry rootDSE = new DirectoryEntry( LDAP:// RootDSE);

string defaultContext = rootDSE.Properties [ defaultNamingContext] [0] .ToString();

//绑定到默认命名上下文-如果*知道*您要绑定的位置-
//您可以立即使用该信息
DirectoryEntry domainRoot = new DirectoryEntry( LDAP:// + defaultContext);

//根据默认的命名上下文条目设置目录搜索器
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);

// SearchScope:OneLevel =仅直属下属(顶级OU);
//子树=整个域中的所有OU(可能需要**很长的时间!)
ouSearcher.SearchScope = SearchScope.OneLevel;
// ouSearcher.SearchScope = SearchScope.Subtree;

//定义要加载的属性-在这里,我只是获得 OU属性,即OU的名称。
ouSearcher.PropertiesToLoad.Add( ou);

//定义过滤器-仅选择组织单位
ouSearcher.Filter =(objectCategory = organizationalUnit);

//执行搜索并遍历结果
foreach(ouSearcher.FindAll()中的SearchResult deResult)
{
字符串ouName = deResult.Properties [ ou ] [0] .ToString();
}

如果您拥有域名(例如, mycompany。 com ),则LDAP根域通常 称为 dc = mycompany,dc = com -这是一个约定,但不一定非要那样。这就是为什么我要连接到 LDAP:// RootDSE 虚拟LDAP根目录并读出属性默认命名上下文这为我提供了默认的LDAP路径。



如果您知道要连接的位置-可以跳过第一步,仅提供有效的LDAP路径(例如 LDAP:// dc = YourCompany,dc = co,dc = jp 或其他)来创建 domainRoot 目录条目。 / p>

i want to get list of OU from Active Directory.

i have only domain name.

how can i achieve this using c#?

解决方案

Try something like this:

// 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();
}

If you have a domain name (e.g. mycompany.com), then the LDAP root domain typically will be called dc=mycompany,dc=com - that's a convention, it doesn't have to be that way though. That's why I'm connecting to the LDAP://RootDSE virtual LDAP root and I read out the property Default Naming Context which gives me the default LDAP path.

If you know where you want to connect to - feel free to skip that first step and just provide the valid LDAP path (e.g. LDAP://dc=YourCompany,dc=co,dc=jp or whatever) to create the domainRoot directory entry.

这篇关于如何使用c#使用DomainName获取AD中OU名称的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆