如何使用c#使用DomainName获取AD中的OU名称列表?
我想从Active Directory获取OU列表 我只有域名如何使用c#使用DomainName获取AD中的OU名称列表?,c#,active-directory,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(
如何使用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
virtualldap根目录,并读取属性Default Naming Context
,它为我提供了默认的LDAP路径
如果您知道要连接到哪里-请跳过第一步,只需提供有效的LDAP路径(例如
LDAP://dc=YourCompany、dc=co、dc=jp
或其他)即可创建domainRoot
目录条目。在项目中添加对System.DirectoryServices的引用
public static List<string> ListOu()
{
List<string> ous = new List<string>();
using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=DOMAIN,dc=COM"))
{
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.Filter = "(&(objectClass=organizationalUnit))";
searcher.SearchScope = SearchScope.Subtree;
searcher.PropertiesToLoad.Add("distinguishedName");
var result = searcher.FindAll();
foreach (SearchResult entry in result)
{
ous.Add(entry.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString());
}
result.Dispose();
searcher.Dispose();
}
return ous;
}
publicstaticlist列表头()
{
List ous=新列表();
使用(DirectoryEntry root=newdirectoryEntry(“LDAP://dc=DOMAIN,dc=COM”))
{
DirectorySearcher search=新的DirectorySearcher(根目录);
searcher.Filter=“(&(objectClass=organizationalUnit))”;
searcher.SearchScope=SearchScope.Subtree;
searcher.PropertiesToLoad.Add(“DiscrimitedName”);
var result=searcher.FindAll();
foreach(结果中的SearchResult条目)
{
添加(entry.GetDirectoryEntry().Properties[“DifferentizedName”].Value.ToString());
}
result.Dispose();
searcher.Dispose();
}
回归理性;
}
所有OU,或仅根目录下的顶级OU?可能重复@Marc:But,ouSearcher.SearchScope=SearchScope.OneLevel代码>此行给出错误。错误是CS0104:“SearchScope”是“System.DirectoryServices.SearchScope”和“System.DirectoryServices.Protocols.SearchScope”之间的模糊引用。
@SanjuMonu:阅读错误消息!它清楚地说明了问题所在(您引用的是两个名称空间,这两个名称空间都有一个名为SearchScope
)的类)。如果您确实需要两个名称空间,那么您需要使用:SearchScope=System.DirectoryServices.SearchScope.OneLevel
…(objectClass=organizationalUnit)