2009-07-07 10 views
6

私は、私たちのユーザーに関するプロパティをすべて前もって指定せずにLDAPクエリを達成しようとしています。これをテーブルに表示したいので、以下のコードを使用します。これは、search.PropertiesToLoad.Add( "cn")のコメントを外すと機能します。同じ方法で追加する他のプロパティは表示されますが、すべてのプロパティを完全に検索すると表示されません。Active Directoryテーブル内のすべてのプロパティを表示

DirectoryEntry myLdapConnection = createDirectoryEntry(); 
DirectorySearcher search = new DirectorySearcher(myLdapConnection); 

search.CacheResults = true; 
//search.PropertiesToLoad.Add("cn"); 

SearchResultCollection allResults = search.FindAll(); 
DataTable resultsTable = new DataTable("Results"); 

//add columns for each property in results 
foreach (string colName in allResults.PropertiesLoaded) 
    resultsTable.Columns.Add(colName, colName.GetType()); 

//loop to add records to DataTable 
foreach (SearchResult result in allResults) 
{ 
    int tmp = result.Properties.Count; 
    DataRow row = resultsTable.NewRow(); 
    foreach (string columnName in search.PropertiesToLoad) 
    { 
     if (columnName.Equals("lastlogon")) 
     { 
      if (result.Properties.Contains(columnName)) 
       row[columnName] = ConvertDate(result.Properties[columnName].ToString()); 
      else 
       row[columnName] = ""; 
     } 
     else 
     { 
      if (result.Properties.Contains(columnName)) 
       row[columnName] = result.Properties[columnName][0].ToString(); 
      else 
       row[columnName] = ""; 
     } 
    } 
    resultsTable.Rows.Add(row); 
} 

gridResults.DataSource = resultsTable; 

問題は何PropertiesToLoadが指定されていなかったが、それは私が欲しいものを達成するために彼らの方法でないとき、私はループにすべてのプロパティをこのことを期待

foreach (string colName in allResults.PropertiesLoaded) 
    resultsTable.Columns.Add(colName, colName.GetType()); 

であると思われます。

私は、まだコードの中にいくつかのキャッチと他のビットを試す必要があることを知っています、それは大まかな草案です。

答えて

12

これはDirectoryEntryを使用して行うことができますが、SearchResultCollectionにはすべてのフィールドがあります。あなたはので、アクティブディレクトリ内のすべてのプロパティが(のmemberOfフィールドのような)複数の値を持つことができます、また

DirectoryEntry entry = result.GetDirectoryEntry(); 

は、それがすべてのアクティブディレクトリのプロパティを持っている必要があり、すべての検索結果の DirectoryEntryを作成するようにしてくださいそれらも同様に繰り返す必要があります。
私は同様のメソッドを書いていますが、キー/値を持つ Listを選択しました(WCFよりも扱いやすいようです。 ILookupが最適ですが、ここで動作させることはできません)。これは、try/catch/usingから削除されました

var list = new List<KeyValuePair<string, string>>(); 
foreach (PropertyValueCollection property in entry.Properties) 
    foreach (object o in property) 
    { 
     string value = o.ToString(); 
     list.Add(new KeyValuePair<string, string>(property.PropertyName, value)); 
    } 
10

あなたはすべてのプロパティを通じてこのように繰り返すことができます。

foreach (SearchResult searchResult in allResults) 
{ 
    foreach (string propName in searchResult.Properties.PropertyNames) 
    { 
    ResultPropertyValueCollection valueCollection = 
    searchResult.Properties[propName]; 
    foreach (Object propertyValue in valueCollection) 
    { 
    Console.WriteLine("Property: " + propName + ": " + propertyValue.ToString()); 
    } 
    } 
} 

は何が必要ということですか?

+0

ありがとう!!!!!!!!! – Sak

1

これを行う方法を見ながら、このスレッドを実行しました。 代わりに、私はそれを行う別の方法を見つけ、うまく動作しているようです。

return ((DirectoryEntry)UserPrincipal.Current.GetUnderlyingObject()).Properties.PropertyNames; 

このロードは、コンボボックスには完全に見つかります。 他の誰かがこのスレッドに出会うだけです。

+0

あなたのコメントがUserPrincipalをDirectorySearcherに関連づけているか、または何か関連性のあるものであれば、これはもっと役に立ちます。私はこのオブジェクトについて十分に分かっていないかもしれませんが、有効なオブジェクトタイプがないので、解決できません。 – Taersious

+0

これは、新しいSystem.DirectoryServices.AccountManagementを使用しています。だから、あなたは参照とusingステートメントを追加する必要があります。 – Michael

関連する問題