我正在尝试从 LDAP 获取所有组及其相关的子组和成员。
我正在使用这段代码:
public List<Group> GetGroups()
{
DirectoryEntry de = new DirectoryEntry("LDAP://someLdap.com");
DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = "(&(objectClass=group))";
searcher.PageSize = 500;
searcher.PropertiesToLoad.Add("sAMAccountName");
searcher.PropertiesToLoad.Add("description");
searcher.PropertiesToLoad.Add("memeber");
searcher.PropertiesToLoad.Add("memeberOf");
searcher.SearchScope = SearchScope.Subtree;
List<Group> groups;
using (SearchResultCollection results = searcher.FindAll())
{
int count = 1;
groups = new List<Group>();
foreach (SearchResult result in results)
{
Group group = new Group();
if (result == null) continue;
Console.WriteLine(count++); // just to print
group.Name = result.Properties["sAMAccountName"][0].ToString();
group.Description = result.Properties["description"][0].ToString();
ResultPropertyValueCollection valueCollection =
result.Properties["memeber"];
foreach (var user in valueCollection)
{
group.Users.Add(user.ToString());
}
groups.Add(group);
}
}
return groups;
}
该代码仅适用于sAMAccountName
和description
但我无法获得全部member
和memberOf
价值
有任何想法吗?