2011-03-18 4 views

답변

3
List<PlayerBO> source = new List<PlayerBO>(); 

DirectoryEntry root = new DirectoryEntry("LDAP://app.shgbit.com"); 
DirectoryEntry gbvision = root.Children.Find("OU=UMP"); 

DirectorySearcher searcher = new DirectorySearcher(gbvision); 
searcher.Filter = "(objectClass=computer)"; 

int index = 1; 

foreach (SearchResult each in searcher.FindAll()) 
{ 
    var box = each.GetDirectoryEntry(); 
    source.Add(new PlayerBO { Id = index++, Name = box.Properties["name"].Value.ToString(), Description = box.Properties["description"].Value.ToString() }); 
} 

ListViewAD.ItemsSource = new SelectableSource<PlayerBO>(source); 
+3

, ** ** 그 라인을 강조하시기 바랍니다 내가 n 텍스트 편집기를 선택하고 편집기 툴바에서 "코드 샘플"버튼 ('{}) '을 클릭하여 멋지게 형식을 지정하고 구문을 강조 표시하십시오! –

15

당신은 System.DirectoryServices에서 적절한 DirectorySearcher를 사용해야합니다, 당신은 organizationalUnit AD 클래스를 검색해야 (- 훨씬 더 빨리 objectClass를 사용하는 것보다 내가 단일 값과 인덱스입니다 objectCategory를 기반으로 검색하는 것이 좋습니다) -이 같은 :

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

DirectoryEntry startingPoint = new DirectoryEntry("LDAP://DC=YourCompany,DC=com"); 

DirectorySearcher searcher = new DirectorySearcher(startingPoint); 
searcher.Filter = "(objectCategory=organizationalUnit)"; 

foreach (SearchResult res in searcher.FindAll()) 
{ 
    orgUnits.Add(res.Path); 
} 
3

나는이 스레드가 조금 오래 알고 있지만, 나는 최근에 DirectorySearcher가 제공하는 것보다 DirectoryEntries을 통해 작전의보다 효율적인 방법을 만들어이 구글의 상단 결과가 이후 공유하고 싶었다. 이 예에서는 처음에 지정된 시작 지점을 기반으로 OU 구조를 복제합니다.

이 첫 번째 생성자에 전달 된 DN 경로의 형식은 "LDAP : // OU = StartingOU, DC = 테스트, DC = COM"에 있어야

using System.DirectoryServices; 
using System.Threading.Tasks; 

public class ADTree 
{ 
    DirectoryEntry rootOU = null; 
    string rootDN = string.Empty; 
    List<ADTree> childOUs = new List<ADTree>(); 

    public DirectoryEntry RootOU 
    { 
     get { return rootOU; } 
     set { rootOU = value; } 
    } 

    public string RootDN 
    { 
     get { return rootDN; } 
     set { rootDN = value; } 
    } 

    public List<ADTree> ChildOUs 
    { 
     get { return childOUs; } 
     set { childOUs = value; } 
    } 

    public ADTree(string dn) 
    { 
     RootOU = new DirectoryEntry(dn); 
     RootDN = dn; 
     BuildADTree().Wait(); 
    } 

    public ADTree(DirectoryEntry root) 
    { 
     RootOU = root; 
     RootDN = root.Path; 
     BuildADTree().Wait(); 
    } 

    private Task BuildADTree() 
    { 
     return Task.Factory.StartNew(() => 
     { 
      object locker = new object(); 
      Parallel.ForEach(RootOU.Children.Cast<DirectoryEntry>().AsEnumerable(), child => 
      { 
       if (child.SchemaClassname.Equals("organizationalUnit")) 
       { 
        ADTree ChildTree = new ADTree(child); 
        lock (locker) 
        { 
         ChildOUs.Add(ChildTree); 
        } 
       } 
      }); 
     }); 
    } 
} 

구축하려면, 당신이 할 필요가있다 다음

ADTree Root = null; 

Task BuildOUStructure = Task.Factory.StartNew(() => 
{ 
    ADTree = new ADTree("LDAP://ou=test,dc=lab,dc=net"); 
}); 

BuildOUStructure.Wait(); 
0

이 내 솔루션이며 작업 : 당신은 코드, XML 또는 데이터 샘플을 게시 할 경우

List<string> DisplayedOU = new List<string>(); 
int step = 0; 
string span = "<span style='margin-left:6px;'> -- </span>"; 

private void getOU2() 
{ 
    string strRet = ""; 
    DirectoryEntry domainRoot = new DirectoryEntry("LDAP://uch.ac/OU=ALL,DC=uch,DC=ac", "user", "pass"); 

    // set up directory searcher based on default naming context entry 
    DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot); 

    // SearchScope: OneLevel = only immediate subordinates (top-level OUs); 
    // subtree = all OU's in the whole domain (can take **LONG** time!) 
    ouSearcher.SearchScope = SearchScope.Subtree; 
    // ouSearcher.SearchScope = SearchScope.Subtree; 

    // define properties to load - here I just get the "OU" attribute, the name of the OU 
    ouSearcher.PropertiesToLoad.Add("ou"); 

    // define filter - only select organizational units 
    ouSearcher.Filter = "(objectCategory=organizationalUnit)"; 

    int cnt = 0; 


    foreach (SearchResult deResult in ouSearcher.FindAll()) 
    { 
     string temp = deResult.Properties["ou"][0].ToString(); 

     strRet += FindSubOU(deResult.Properties["adspath"][0].ToString(), cnt); 

    } 

    Literal1.Text = strRet; 
} 


private string FindSubOU(string OU_Path, int cnt) 
{ 
    string strRet = ""; 

    DirectoryEntry domainRoot = new DirectoryEntry(OU_Path, "user", "pass"); 

    // set up directory searcher based on default naming context entry 
    DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot); 

    // SearchScope: OneLevel = only immediate subordinates (top-level OUs); 
    // subtree = all OU's in the whole domain (can take **LONG** time!) 
    ouSearcher.SearchScope = SearchScope.Subtree; 
    // ouSearcher.SearchScope = SearchScope.Subtree; 

    // define properties to load - here I just get the "OU" attribute, the name of the OU 
    ouSearcher.PropertiesToLoad.Add("ou"); 

    // define filter - only select organizational units 
    ouSearcher.Filter = "(objectCategory=organizationalUnit)"; 

    //adspath 
    // do search and iterate over results 
    foreach (SearchResult deResult in ouSearcher.FindAll()) 
    { 
     string temp = deResult.Properties["ou"][0].ToString(); 

     if (!DisplayedOU.Contains(deResult.Properties["ou"][0].ToString())) 
     { 
      string strPerfix = ""; 

      for (int i = 0; i < step; i++) 
       strPerfix += span; 

      strRet += strPerfix + ++cnt + ". " + deResult.Properties["ou"][0].ToString() + " ----> " + deResult.Properties["adspath"][0].ToString() + "<br />"; 

      DisplayedOU.Add(deResult.Properties["ou"][0].ToString()); 

      step++; 

      strRet += FindSubOU(deResult.Properties["adspath"][0].ToString(), cnt); 

      step--; 
     } 

    } 


    return strRet; 
} 
관련 문제