Active Directory Display all properties in a table

后端 未结 3 1076
悲&欢浪女
悲&欢浪女 2021-02-16 00:06

I am trying to achieve an LDAP query to gather all properties we have about our users without specifying the properties before hand, I would like to display this in a table so u

相关标签:
3条回答
  • 2021-02-16 00:22

    You can iterate through all properties this way:

    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());
        }
      }
    }
    

    Is that what you need?

    0 讨论(0)
  • 2021-02-16 00:26

    This can be done using DirectoryEntry, but I don't think the SearchResultCollection has all fields.
    Try to create a DirectoryEntry for every search result, it should have all active directory properties:

    DirectoryEntry entry = result.GetDirectoryEntry();
    

    Also, note that in the active directory every property can have multiple values (like the MemberOf field), so you'll have to iterate them as well.
    I've wrote a similar method, but I chose a List with keys/values (it seemed more manageable over WCF. ILookup would be optimal, but I couldn't get it to work here). Here it is, stripped from 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));
       }
    
    0 讨论(0)
  • 2021-02-16 00:33

    Came across this thread while looking on how to do this myself. Instead i found a different way of doing it, and seems to be working fine.

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

    Its loading perfectly find in a Combobox anyhow. Just incase anyone else comes across this thread.

    0 讨论(0)
提交回复
热议问题