查找LDAP查询以仅从Active Directory中获取具有组的OU。
最重要的是只使用LDAP查询,我不想使用C#代码过滤每个OU。
由于
答案 0 :(得分:0)
组可以存储在organizationalUnits中,也可以存储在域,容器中。
使用DirectoryEntry或AccountManagement,您可以执行以下操作:
以下是使用System.DirectoryServices.AccountManagement
和System.DirectoryServices
/* Retreiving a principal context
*/
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");
/* Look for all the groups from the root
*/
GroupPrincipal allGroups = new GroupPrincipal(domainContext);
allGroups.Name = "*";
/* Bind a searcher
*/
PrincipalSearcher searcher = new PrincipalSearcher();
searcher.QueryFilter = allGroups;
PrincipalSearchResult<Principal> hRes = searcher.FindAll();
/* Read The result
*/
List<DirectoryEntry> listContainerWithGroups = new List<DirectoryEntry>();
foreach (GroupPrincipal grp in hRes)
{
DirectoryEntry deGrp = grp.GetUnderlyingObject() as DirectoryEntry;
if (deGrp != null)
listContainerWithGroups.Add(deGrp.Parent);
}
/* Get Unique Entries
*/
var listContainerWithGroupsUnique = from o in listContainerWithGroups
group o by o.Properties["distinguishedName"].Value into dePackets
select dePackets.First();
foreach (DirectoryEntry deTmp in listContainerWithGroupsUnique)
{
Console.WriteLine(deTmp.Properties["distinguishedName"].Value);
}
答案 1 :(得分:0)
单次搜索无法做到这一点。您需要获取每个OU,然后对该OU执行一级搜索(&amp;(objectCategory = group)(objectClass = group))。当您考虑可能需要执行多少次搜索时,这不会特别有效。还要考虑是否需要处理OU = A \ OU = B的场景。如果OU = B包含该组,您是否包括OU = A(父级)?