我写这篇文章是为了让特定用户所属的群组属于:
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中检索所有组,而不仅仅是针对特定用户?
答案 0 :(得分:2)
如何为组设置过滤器并枚举结果。
试试这个过滤器:
AD.Children.SchemaFilter.Add("group");
答案 1 :(得分: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版本 - &gt; http://anyrest.wordpress.com/2010/06/28/active-directory-c/
旧版本 - &gt; http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/