2016-12-08 4 views
0

Active Directory에서 특정 사용자를 검색하려고하는데 FindAll 메서드를 사용하고 있습니다. 내가 호출 할 때 오류가 발생했습니다. 이 문제를 어떻게 해결할 수 있습니까? C# FindAll 메서드를 사용하여 Active Directory에서 특정 사용자를 찾을 수 없습니다.

내가 점점 오전 예외 : ' 함께 System.Runtime.InteropServices.COMException'유형의

처리되지 않은 예외

SearchResultCollection sResults = null; 
    try 
    { 
     //modify this line to include your domain name 
     string path = "LDAP://microsistemas.com"; 
     //init a directory entry 
     DirectoryEntry dEntry = new DirectoryEntry(path); 

     //init a directory searcher 
     DirectorySearcher dSearcher = new DirectorySearcher(dEntry); 

     //This line applies a filter to the search specifying a username to search for 
     //modify this line to specify a user name. if you want to search for all 
     //users who start with k - set SearchString to "k"; 
     dSearcher.Filter = "(&(objectClass=user))"; 

     //perform search on active directory 
     sResults = dSearcher.FindAll(); 

     //loop through results of search 
     foreach (SearchResult searchResult in sResults) 
     { 
      if (searchResult.Properties["CN&"][0].ToString() == "Administrator") 
      { 
       ////loop through the ad properties 
       //foreach (string propertyKey in 
       //searchResult.Properties["st"]) 
       //{ 

       //pull the collection of objects with this key name 
       ResultPropertyValueCollection valueCollection = 
       searchResult.Properties["manager"]; 

       foreach (Object propertyValue in valueCollection) 
       { 

        //loop through the values that have a specific name 
        //an example of a property that would have multiple 
        //collections for the same name would be memberof 
        //Console.WriteLine("Property Name: " + valueCollection..ToString()); 
        Console.WriteLine("Property Value: " + (string)propertyValue.ToString()); 

        //["sAMAccountName"][0].ToString(); 
       } 
       //} 
       Console.WriteLine(" "); 
      } 
     } 
    } 
    catch (InvalidOperationException iOe) 
    { 
     // 
    } 
    catch (NotSupportedException nSe) 
    { 
     // 
    } 
    finally 
    { 

     // dispose of objects used 
     if (sResults != null) 
      sResults.Dispose(); 

    } 
    Console.ReadLine(); 
} 
+0

당신은'PrincipalContext'을 잘 알고있는 ...? 또한 왜'' "''vs dsearcher.Filter = " (& (objectClass = user))을 사용하여 필터 텍스트를 래핑하지 않는지 "; – MethodMan

답변

0

를 System.DirectoryServices.dll 발생 .NET 3.5 이상을 사용하는 경우 System.DirectoryServices.AccountManagement (S.DS.AM) 네임 스페이스를 확인해야합니다.

// set up domain context - limit to the OU you're interested in 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "DC=microsistemas,DC=com")) 
{ 
    // find a user 
    Principal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

    if(user != null) 
    { 
     // do something here....  
    } 
} 

새로운 S.DS.AM 그것이 정말 쉽게 AD에서 사용자 및 그룹에 함께 놀러 할 수 있습니다 : 기본적으로

, 당신은 AD에서 사용자 및/또는 그룹을 쉽게 찾을 도메인 컨텍스트를 정의 할 수 있습니다 !

여기에 대해 자세히 알아보기 :

관련 문제