2011-08-30 14 views
13

Belirli bir AD grubundaki tüm üyelerin/kullanıcıların bir listesini almanın ve bir kullanıcının etkin (veya devre dışı) olup olmadığını belirlemenin en hızlı yolu nedir?Active Directory Group üyelerini alın ve etkinleştirilmiş veya devre dışı bırakılmış olup olmadıklarını kontrol edin

Potansiyel olarak 20K kullanıcılarından bahsediyoruz, bu yüzden her bir kullanıcı için AD'ye çarpmamaktan kaçınmak istiyorum.

+0

.NET 4'te olduğumu eklemeyi unuttum (alternatif olarak 3.5) –

cevap

30

.NET 3.5 ve üstü sürümleri kullanıyorsanız, System.DirectoryServices.AccountManagement (S.DS.AM) ad alanını gözden geçirmelisiniz. Hepsi burada! okuyunuz:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find the group in question 
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere"); 

// if found.... 
if (group != null) 
{ 
    // iterate over members 
    foreach (Principal p in group.GetMembers()) 
    { 
     Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); 

     // do whatever you need to do to those members 
     UserPrincipal theUser = p as UserPrincipal; 

     if(theUser != null) 
     { 
      if(theUser.IsAccountLockedOut()) 
      { 
       ... 
      } 
      else 
      { 
       ... 
      } 
     } 
    } 
} 

: MSDN docs on System.DirectoryServices.AccountManagement

Temelde

+0

Teşekkürler, bunu kontrol edeceğim. –

+2

Bunu kullanan herkes için not: Bu, geçişli grup üyelikleri ile çalışmaz, yani Grup B, Grup A üyesiyse ve Kullanıcı C, Grup B'nin bir üyesiyse, sonuçta C Kullanıcıları gösterilmez. –

+0

Alan adı, kullanıcı adı ve pswd nerede belirtilir? – Shesha

1

Lütfen aşağıdaki kodu deneyebilirsiniz. Bir LDAP sorgusunda ve yinelemeli olarak istediğiniz şeyi almak için Search Filter Syntax kullanın. İlgi, sorgunun sunucuda yapılmasıdır. Bunun, @marc_s çözümünden daha hızlı olduğundan emin değilim, ancak var olan ve .NET 2.0 (W2K3 SP2 başlangıcı) çerçevesi üzerinde çalışıyor.

string sFromWhere = "LDAP://WM2008R2ENT:389/dc=dom,dc=fr"; 
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "dom\\jpb", "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) 
* coupled with LDAP_MATCHING_RULE_BIT_AND on userAccountControl with ACCOUNTDISABLE 
*/ 
DirectorySearcher dsLookFor = new DirectorySearcher(deBase); 
dsLookFor.Filter = "(&(memberof:1.2.840.113556.1.4.1941:=CN=MonGrpSec,OU=MonOu,DC=dom,DC=fr)(userAccountControl:1.2.840.113556.1.4.803:=2))"; 
dsLookFor.SearchScope = SearchScope.Subtree; 
dsLookFor.PropertiesToLoad.Add("cn"); 

SearchResultCollection srcUsers = dsLookFor.FindAll(); 

/* Just to know if user is present in an other group 
*/ 
foreach (SearchResult srcUser in srcUsers) 
{ 
    Console.WriteLine("{0}", srcUser.Path); 
} 
İlgili konular