2017-05-19 12 views
0

私はADで特定のユーザーのクエリを行い、複数のプロパティのリストを作成しようとしています。以下のコード。 searchResult.Properties ["manager"]またはsearchResult.Properties ["mail"]を実行すると、正しい結果が得られます。しかし、私はどのように複数のプロパティを検索するのだろうか?は、Active Directoryで複数のプロパティを検索するのに役立つ必要があります

DirectoryEntry dEntry = new DirectoryEntry(path);  

       DirectorySearcher dSearcher = new DirectorySearcher(dEntry);      

       dSearcher.Filter = "(&(ObjectClass=user)(samaccountname=mcavanaugh))";  

       sResults = dSearcher.FindAll(); 

       foreach (SearchResult searchResult in sResults) 
       { 
        var sAMAccountName = searchResult.Properties["samaccountname"][0].ToString().ToLower(); 
        if (sAMAccountName == "mcavanaugh") 
        { 
         //Right here is where i would select multiple ad properties 
         ResultPropertyValueCollection valueCollection = searchResult.Properties["manager, mail"]; 

         foreach (Object propertyValue in valueCollection) 
         { 
          var PropertyName = (string)propertyValue.ToString(); 
          testlist.Text = PropertyName; 
         } 
        } 
       } 

答えて

0

Propertiesプロパティには、複数のプロパティに同時にアクセスするオプションがありません。異なる特性からの値を混合することは賢明に見えません。あなたの最善の解決策は、foreachを2回実行して、おそらくDRY機能を作成することです。私は最近、これに取り組んできたあなたはif

if (sAMAccountName == "mcavanaugh") { 
    addPropertyValues(searchResult, testlist, "manager"); 
    addPropertyValues(searchResult, testlist, "mail"); 
} 
0

に使用することができます

void addPropertyValues<T>(SearchResult sr, T testlist, string propName) { 
    foreach (var pv in sr[propName]) { 
     testlist.Text = pv.ToString(); 
    } 
} 

。私は複数のプロパティを配置する場所を見つけ、それらをプロパティに割り当てる配列に配置しました。それはこれまでかなりうまく動いています。 :

DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://DC=demo,DC=Com"); 

DirectorySearcher search = new DirectorySearcher(myLdapConnection); 
search.Filter = "(sAMAccountName=" + username + ")"; 

string[] requiredProperties = new string[] { "cn", "Displayname", "Title", "Department", "l", "Homedirectory", "telephoneNumber", "lockoutTime", "badlogoncount", "passwordexpired", "badPasswordTime", "whenCreated", "sAMAccountName", "pwdLastSet", "thumbnailPhoto", "givenName", "sn", "mail", "msRTCSIP-PrimaryUserAddress", "distinguishedName", "manager" }; 


foreach(String property in requiredProperties) 
search.PropertiesToLoad.Add(property); 

//next code will output to a usertextbox that I had set up in a Form. You can convert to console.writeline 

if (searchResult != null) { 
    foreach(String property in requiredProperties) 
    foreach(Object myCollection in searchResult.Properties[property]) 
    UserTextbox.Text += "\r\n" + (String.Format("{0,0} : {1} : {2}", property, myCollection.ToString(), myCollection.GetType().ToString())); 
} 
関連する問題