2012-02-28 2 views
3

DirectoryServices 네임 스페이스를보고 있는데 광고의 모든 그룹 목록을 가져 와서 목록 상자에로드하려고합니다.Active Directory에 모든 그룹 및 그룹 구성원을 쿼리하는 방법?

그룹을 선택하면 해당 그룹에 할당 된 모든 사용자가 포함 된 다른 목록 상자와 관리자 이름이 포함 된 텍스트 상자를 채우고 싶습니다. 나는 그 과정에서 머리를 감싸는 데 어려움을 겪고있다. 누군가 나를 도울 수 있었습니까?

전체 예를 들어 보면 더 큰 그림을 더 잘 이해할 수있을 것이라고 확신합니다. 당신이 이미하지 않은 경우

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    GroupPrincipal foundGroup = found as GroupPrincipal; 

    if(foundGroup != null) 
    { 
     // do whatever you need to do, e.g. put name into a list of strings or something 
    } 
} 

: TIA

+1

이 참조 자료를 보았습니까? : http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C# – Dan

+0

네,하지만 정확히 어떤 것인지 모르겠습니다. 내게 적용되거나 내 목표에 적용하는 방법 – Sinaesthetic

답변

9

당신이 .NET 3.5 이상에서 실행하는 경우, 당신은 당신의 검색을 할 수있는 PrincipalSearcher하고 "쿼리별로 예를 들어"주체를 사용할 수 있습니다 - 절대적으로 주어진 그룹이있을 때 System.DirectoryServices.AccountManagement

의 새로운 기능을 최대한 활용하는 방법을 잘 보여줍니다 MSDN 문서 Managing Directory Security Principals in the .NET Framework 3.5을 읽고, 당신은 쉽게 사용하여 모든 그룹 구성원을 얻을 수 있습니다 :

// find the group in question (or load it from e.g. your list) 
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 
    } 
} 
사용자가 및/또는 관리자가 있는지를 판단 할 수 있도록하는 어떤 멤버를 노출

PrincipalContext context = new PrincipalContext(ContextType.Domain); 
GroupPrincipal queryPrincipal = new GroupPrincipal(context); 

using (PrincipalSearcher searcher = new PrincipalSearcher(queryPrincipal)) 
using (PrincipalSearchResult<Principal> allPrincipals = searcher.FindAll()) 
    foreach (GroupPrincipal groupPrincipal in allPrincipals.OfType<GroupPrincipal>()) 
    { 
     // Process group... 

     foreach (UserPrincipal userPrincipal in groupPrincipal.Members.OfType<UserPrincipal>()) 
     { 
      // Process group member... 
     } 
    } 

UserPrincipal class이 표시되지 않는 :

2

은 내가 System.DirectoryServices.AccountManagement 네임 스페이스를 사용 marc_s 유사한 무언가를 내놓았다 오히려,

DirectoryEntry userEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry; 

if (userEntry != null) 
{ 
    bool isManager = userEntry.Properties["directReports"].Count > 0; 
    bool isManaged = userEntry.Properties["manager"].Count > 0; 

    // Perform further processing... 
} 

당신은 사용자가 현재보고있는 그룹의 관리자가 있는지 확인하려면,하지만, 몇 가지 추가 논리가 필요합니다하지만 당신은 여전히 ​​사용자의 DirectoryEntry를 획득하여 작업을 수행 할 수 있습니다 다른 그룹보다. 어쩌면 directReports 속성을 검사하여 현재 사용자 그룹에 속한 사용자가 있는지 확인할 수 있습니다.

관련 문제