2015-07-10 83 views
2

我正在使用以下方法查詢來自大AD組的成員。使用DirectorySearcher從大AD組中獲取SamAccountName

try 
{ 
    DirectoryEntry entry = new DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com"); 
    DirectorySearcher searcher = new DirectorySearcher(entry); 
    searcher.Filter = "(objectClass=*)"; 

    uint rangeStep = 1000; 
    uint rangeLow = 0; 
    uint rangeHigh = rangeLow + (rangeStep - 1); 
    bool lastQuery = false; 
    bool quitLoop = false; 

    do 
    { 
     string attributeWithRange; 
     if(!lastQuery) 
     { 
      attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh); 
     } 
     else 
     { 
      attributeWithRange = String.Format("member;range={0}-*", rangeLow); 
     }   
     searcher.PropertiesToLoad.Clear(); 
     searcher.PropertiesToLoad.Add(attributeWithRange); 
     SearchResult results = searcher.FindOne(); 
     foreach(string res in results.Properties.PropertyNames) 
     { 
      System.Diagnostics.Debug.WriteLine(res.ToString()); 
     } 
     if(results.Properties.Contains(attributeWithRange)) 
     { 
      foreach(object obj in results.Properties[attributeWithRange]) 
      { 
       Console.WriteLine(obj.GetType()); 
       if(obj.GetType().Equals(typeof(System.String))) 
       { 
       } 
       else if (obj.GetType().Equals(typeof(System.Int32))) 
       { 
       } 
       Console.WriteLine(obj.ToString()); 
      } 
      if(lastQuery) 
      { 
       quitLoop = true; 
      } 
     } 
     else 
     { 
      lastQuery = true; 
     } 
     if(!lastQuery) 
     { 
      rangeLow = rangeHigh + 1; 
      rangeHigh = rangeLow + (rangeStep - 1); 
     } 
    } 
    while(!quitLoop); 
} 
catch(Exception ex) 
{ 
    // Handle exception ex. 
} 

這裏我想檢索SamAccountName的成員以及他們的distinguishedName.Also我也有一些組包含跨域成員。能否請你幫忙。

+0

任何幫助將不勝感激 – gokul731

+0

你能發佈你的最終版本嗎?答案有幫助嗎? –

回答

2

要獲取任何屬性的值,您通常需要調用DirectoryEntry.Properties [屬性名稱] .Value(例如,

var x = results.Properties["distinguishedName"].Value 

對於它可能需要提供索引屬性[屬性名] [X] .value的

如果值不是默認的結果填充,您可能需要強制某些索引值使用DirectorySearcher.PropertiesToLoad.Add加載(PropertyName);