2011-03-18 8 views
7

मैंने DirectoryServices कक्षा में देखा है और ऐसा लगता है कि मुझे क्या चाहिए, लेकिन मुझे संगठनात्मक इकाइयों का संग्रह लाने के लिए आवश्यक कक्षाएं/विधियां नहीं मिल रही हैं।सक्रिय निर्देशिका से संगठनात्मक इकाइयों की सूची कैसे प्राप्त कर सकता हूं?

क्या आप कुछ सुझाव दे सकते हैं?

उत्तर

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 ई वर्ग के लिए खोज करने के लिए की जरूरत है (मैं objectCategory जो एकल-मान और इंडेक्स के आधार पर खोज की सिफारिश करेंगे - objectClass का उपयोग करने से बहुत तेजी से) - कुछ इस तरह:

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 के माध्यम से चालों की एक अधिक कुशल तरीका बनाया है और साझा करने के लिए के बाद से इस गूगल पर शीर्ष परिणाम था चाहता था। यह उदाहरण प्रारंभिक निर्दिष्ट प्रारंभ बिंदु के आधार पर ओयू संरचना को दोहराता है।

डीएन पथ पहले निर्माता के लिए पारित किया प्रारूप "LDAP: // OU = StartingOU, डीसी = परीक्षण, डीसी = कॉम" में होना चाहिए

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

यह मेरा समाधान है और यह काम कर रहा है:

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; 
} 
संबंधित मुद्दे

 संबंधित मुद्दे