2014-03-27 52 views
0
using (DirectoryEntry rootEntry = new DirectoryEntry(ConfigurationKeys.Ldap, string.Empty, string.Empty, AuthenticationTypes.None)) 
{ 
    using (DirectorySearcher adSearch = new DirectorySearcher(rootEntry)) 
    { 
     adSearch.SearchScope = SearchScope.Subtree; 
     adSearch.PropertiesToLoad.Add("givenname"); 
     adSearch.PropertiesToLoad.Add("mail"); 

     adSearch.Filter = "([email protected])"; 
     SearchResult adSearchResult = adSearch.FindOne(); 
    } 
} 

從上面的示例中,檢索屬性「givenname」並將其存儲到字符串變量中的最有效方法是什麼?從LDAP/Active Directory中讀取屬性的最快方法搜索結果

回答

2

既然你已經在性能搜索加載的列表中的屬性,只是訪問屬性的搜索結果:

using (DirectoryEntry rootEntry = new DirectoryEntry(ConfigurationKeys.Ldap, string.Empty, string.Empty, AuthenticationTypes.None)) 
{ 
    using (DirectorySearcher adSearch = new DirectorySearcher(rootEntry)) 
    { 
     adSearch.SearchScope = SearchScope.Subtree; 
     adSearch.PropertiesToLoad.Add("givenname"); 
     adSearch.PropertiesToLoad.Add("mail"); 

     adSearch.Filter = "([email protected])"; 

     SearchResult adSearchResult = adSearch.FindOne(); 

     // make sure the adSearchResult is not null 
     // and the "givenName" property is not null (could be empty/null) 
     if(adSearchResult != null && adSearchResult.Properties["givenName"] != null) 
     { 
      // make sure the givenName property contains at least one string value 
      if (adSearchResult.Properties["givenName"].Count > 0) 
      { 
       string givenName = adSearchResult.Properties["givenName"][0].ToString(); 
      } 
     } 
    } 
}