我写了这个让特定用户所属的组:
I wrote this to get the groups a particular user belongs to:
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
}
这怎么可以检索所有组窗口,而不只是为特定用户?
How is it possible to retrieve all the groups in windows and not just for a particular user?
试试这个一出来,它会给你一个specicied OU中的所有组。
Try this one out, it will give you all groups in a specicied 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;
}
对于一个完整的参考,您可以检查这一个
For a full reference you can check this one out
NET 3.5的版本 - > HTTP:/ /anyrest.word$p$pss.com/2010/06/28/active-directory-c/
.Net 3.5 version - > http://anyrest.wordpress.com/2010/06/28/active-directory-c/
旧版本 - > HTTP ://anyrest.word$p$pss.com/2010/02/01/active-directory-objects-and-c/