我试图从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
值
有什么想法吗?
答案 0 :(得分:1)
那是因为你在整个代码和帖子中拼错了两个属性名称。