Вот способ сделать это рекурсивно, используя DirectotyEntry и Microsoft LDAP_MATCHING_RULE_IN_CHAIN . Я работаю над Framework 2.0 в ActiveDirectory 2003 и 2008 R2
using System.DirectoryServices;
using System.Security.Principal;
static void Main(string[] args)
{
//Connection to Active Directory
string sFromWhere = "LDAP://SRVENTR2:389/dc=societe,dc=fr";
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "societe\\administrateur", "test.2011");
// To find all the users member of groups "Grp1" :
// Set the base to the groups container DN; for example root DN (dc=societe,dc=fr)
// Set the scope to subtree
// Use the following filter :
// (member:1.2.840.113556.1.4.1941:=CN=Grp1,OU=MonOu,DC=X)
//
DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
dsLookFor.Filter = "(&(memberof:1.2.840.113556.1.4.1941:=CN=Grp1,OU=MonOu,DC=societe,DC=fr)(objectCategory=user))";
dsLookFor.SearchScope = SearchScope.Subtree;
dsLookFor.PropertiesToLoad.Add("cn");
SearchResultCollection srcUsers = dsLookFor.FindAll();
// To check user properties
foreach (SearchResult srcUser in srcUsers)
{
Console.WriteLine("{0}", srcUser.Path);
}
Console.ReadLine();
}
Starting Framework 3.5 Вы можете использовать Принципы безопасности каталогов и делать это так:
/* Retreiving a principal context
*/
PrincipalContext context = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "root.123");
DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "WM2008R2ENT:389");
Domain dn = Domain.GetDomain(dc);
//Console.WriteLine("Le nom : {0}", dn.PdcRoleOwner.Domain);
/* Retreive a users from group
*/
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, @"MonGrpSec"))
{
if (group != null)
{
foreach (var p in group.GetMembers(false))
{
Console.WriteLine(p.SamAccountName);
}
}
}