C#

2010-01-20 10 views
2

을 사용하여 Active Directory 그룹의 모든 사용자 검색 특정 AD 그룹에서 사용자를 검색하려면 어떻게합니까?C#

도메인, 사용자 이름 및 암호를 사용하여 PrincipalContext를 인스턴스화하는 것으로 시작합니까?

답변

14

먼저 그룹을 찾으십시오. 그런 다음 GetMembers()를 사용하여 사용자를 열거하십시오.

using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (var group = GroupPrincipal.FindByIdentity(context, "groupname")) 
    { 
      var users = group.GetMembers(true); // recursively enumerate 
      ... 
    } 
} 

.NET 4.0에는 고정되어 있으며 그룹의 1500 명이 넘는 구성원을 열거하지 못합니다. 큰 그룹 인 경우 System.DirectoryServices의 이전 메서드를 활용하여 alternative method을 사용해야합니다.

4

.NET 3.5에서 System.DirectoryServices.AccountManagement으로 할 수있는 작업에 대한 개요는 Managing Directory Security Principals in the .NET Framework 3.5을 확인하십시오.

그룹의 멤버를 검색, 당신은이 작업을 수행 :이 도움이

// build the principal context - use the NetBIOS domain name 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAIN"); 

// get the group you're interested in 
GroupPrincipal group = GroupPrincipal.FindByIdentity("cn=YourGroupname"); 

// iterate over its members 
foreach(Principal p in group.Members) 
{ 
    // do whatever you need to do to its members here    
} 

희망을!

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.DirectoryServices.AccountManagement; 

namespace ExportActiveDirectoryGroupsUsers 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args == null) 
      { 
       Console.WriteLine("args is null, useage: ExportActiveDirectoryGroupsUsers OutputPath"); // Check for null array 
      } 
      else 
      { 
       Console.Write("args length is "); 
       Console.WriteLine(args.Length); // Write array length 
       for (int i = 0; i < args.Length; i++) // Loop through array 
       { 
        string argument = args[i]; 
        Console.Write("args index "); 
        Console.Write(i); // Write index 
        Console.Write(" is ["); 
        Console.Write(argument); // Write string 
        Console.WriteLine("]"); 
       } 
       try 
       { 
        using (var ServerContext = new PrincipalContext(ContextType.Domain, ServerAddress, Username, Password)) 
        { 
         /// define a "query-by-example" principal - here, we search for a GroupPrincipal 
         GroupPrincipal qbeGroup = new GroupPrincipal(ServerContext, args[0]); 

         // 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) 
          { 
           // iterate over members 
           foreach (Principal p in foundGroup.GetMembers()) 
           { 
            Console.WriteLine("{0}|{1}", foundGroup.Name, p.DisplayName); 
            // do whatever you need to do to those members 
           } 
          } 

         } 
        } 
        //Console.WriteLine("end"); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("Something wrong happened in the AD Query module: " + ex.ToString()); 
       } 
       Console.ReadLine(); 
      } 
     } 
    } 
}