2011-12-02 3 views
2

사용자가 텍스트 상자에 그룹 이름을 입력하고 로그인 이름과 SID 만 반환 할 수있게하려고합니다.AD에서 그룹에 속한 사용자를 찾고 SAMAccountName과 SID를 얻는 방법?

지금까지 내가 가지고 있고, 그 그룹에 사용자를로드하지만 로그인과 SID를 추출하는 방법을 모르겠다.

SearchResult result; 
      DirectorySearcher search = new DirectorySearcher(); 
      search.Filter = String.Format("(cn={0})", txtGroup.Text); 
      search.PropertiesToLoad.Add("member"); 
      search.PropertiesToLoad.Add("cn"); 
      search.PropertiesToLoad.Add("objectGUID"); 
      result = search.FindOne(); 


      StringBuilder userNames = new StringBuilder(); 
      if (result != null) 
      { 
       for (int counter = 0; counter < 
       result.Properties["member"].Count; counter++) 
       { 
        string user = (string)result.Properties["member"][counter]; 
        userNames.AppendLine(user); 

       } 
      } 
      lblResults.Text = userNames.ToString(); 

답변

0

나는 당신이 당신의 쿼리를 반대 경우는 잘 작동 할 것이라는 점을 생각한다 :

(&(objectClass=user)(memberOf={0})) 

직접 FindAll를 사용하여 사용자의 목록을 다시 얻을 것이다 이쪽으로. PropertiesToLoadsAMAccountName 등을 추가하는 것을 잊지 마십시오.

1

propertie 느릅 나무는 SID가 objectSid라고 포함하고 propertie은 일 로그인이 NT4 호환 버전 및 userPrincipalName에 대한 sAMAccountName입니다 포함 느릅 나무. @Virkkunen의 조언을 더 잘 활용하십시오.

static void Main(string[] args) 
{ 
    /* Connection to Active Directory 
    */ 
    DirectoryEntry deBase = new DirectoryEntry("LDAP://192.168.183.138:389/dc=societe,dc=fr", "administrateur", "pwd"); 

    /* Directory Search 
    */ 
    DirectorySearcher dsLookForGrp = new DirectorySearcher(deBase); 
    dsLookForGrp.Filter = String.Format("(cn={0})", "yourgroup"); 
    dsLookForGrp.SearchScope = SearchScope.Subtree; 
    dsLookForGrp.PropertiesToLoad.Add("distinguishedName"); 
    SearchResult srcGrp = dsLookForGrp.FindOne(); 

    /* Directory Search 
    */ 
    DirectorySearcher dsLookForUsers = new DirectorySearcher(deBase); 
    dsLookForUsers.Filter = String.Format("(&(objectCategory=person)(memberOf={0}))", srcGrp.Properties["distinguishedName"][0]); 
    dsLookForUsers.SearchScope = SearchScope.Subtree; 
    dsLookForUsers.PropertiesToLoad.Add("objectSid"); 
    dsLookForUsers.PropertiesToLoad.Add("userPrincipalName "); 
    dsLookForUsers.PropertiesToLoad.Add("sAMAccountName"); 
    SearchResultCollection srcLstUsers = dsLookForUsers.FindAll(); 

    foreach (SearchResult sruser in srcLstUsers) 
    { 
    Console.WriteLine("{0}", sruser.Path); 

    SecurityIdentifier sid = new SecurityIdentifier((byte[]) sruser.Properties["objectSid"][0], 0); 
    Console.WriteLine(sid.ToString());  

    foreach (string property in sruser.Properties.PropertyNames) 
    { 
     Console.WriteLine("\t{0} : {1} ", property, sruser.Properties[property][0]); 
    } 
    } 
} 
관련 문제