2012-07-31 2 views
1

Active Directory의 모든 그룹을 얻으려면이 코드를 C#으로 작성하십시오. 내가 등 모든 서버 이름, OU, DC를 통과 할 필요가없는 것처럼 완벽하게 잘 작동Active Directory 특수 그룹을 제외한 모든 그룹을 가져옵니다.

 UserPrincipal current_user = UserPrincipal.Current; 

     PrincipalContext current_context = current_user.Context; 

     PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

     GroupPrincipal qbeUser = new GroupPrincipal(ctx); 

     Principal userOrGroup = qbeUser as Principal; 
     userOrGroup.Name = "*"; 

     PrincipalSearcher searcher = new PrincipalSearcher(userOrGroup); 

     List<string> AllGroups = new List<string>(); 

     // enumerate the results - you need to check what kind of principal you get back 
     foreach (Principal found in searcher.FindAll()) 
     { 
      // is it a UserPrincipal - do what you need to do with that... 
      if (found is UserPrincipal) 
      { 
       // ...... 
      } 
      else if (found is GroupPrincipal) 
      { 
       AllGroups.Add(found.Name); 

       //GroupPrincipal gp = found as GroupPrincipal; 

       //var data = gp.GetMembers(); 

       // if it's a group - do whatever you need to do with a group.... 
      } 
     } 

     //return AllGroups; 

문제는 내가

PerformanceLogUsers, SchemaAdmins, HelpServiceGroups, 텔넷과 같은 필요가없는 너무 많은 그룹을 나열이다 클라이언트 등등.

관리자, 손님 및 다른 사용자가 만든 그룹과 같은 그룹 만 필요합니다. 나는 이것들에 대해 특별한 단체와 기타 등등을 읽었습니다.

이와 관련하여 도움을 주신 모든 분들께 감사드립니다.

답변

1

AD는 검색을 수행 할 때 그룹 관련성에 의해 차별하지 않습니다. 그룹이거나 그렇지 않습니다. 그러나 보안 그룹 또는 메일 그룹을 반환할지 여부를 지정할 수 있습니다.

디렉토리가 현재 어떻게 설정되어 있는지는 또 다른 문제입니다. 원하는 그룹과 원하지 않는 그룹이 모두 "보안 그룹"인 경우 문제가 발생합니다.

하나의 해결책은 관련 그룹에 공통적 인 (또는 하나 생성) 몇 가지 고유 한 속성을 찾은 다음 해당 존재를 필터링하는 것입니다.

관련 문제