2

我写这个来获取特定用户所属的组:

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry user = AD.Children.Find(completeUserName, "user");
object obGroups = AD.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
   // Create object for each group.
    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
    listOfMyWindowsGroups.Add(obGpEntry.Name);
}
for (int j = 0; j < listOfMyWindowsGroups.Count; j++)
{
   //ex
}

如何检索 Windows 中的所有组,而不仅仅是针对特定用户?

4

2 回答 2

2

如何为组设置过滤器并枚举结果。

试试这个过滤器:

AD.Children.SchemaFilter.Add("group");
于 2010-10-25T23:08:43.263 回答
1

试试这个,它将为您提供特定 OU 中的所有组。

public ArrayList GetGroups()
{
    ArrayList myItems = new ArrayList();

    // Create the principal context for the group object.
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);

    // Create the GroupPrincipal object and set the diplay name property. 
    GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);

    // Create a PrincipalSearcher object.     
    PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oGroupPrincipal);

    // Searches for all groups named "Administrators".
    PrincipalSearchResult<Principal> oPrincipalSearchResult = oPrincipalSearcher.FindAll();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}

如需完整参考,您可以查看这个

.Net 3.5 版本-> http://anyrest.wordpress.com/2010/06/28/active-directory-c/

旧版本 - > http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

于 2010-10-26T01:55:22.997 回答