如何限制通过 System.DirectoryServices 在 LDAP 查询中返回的属性?
我一直在使用 DirectorySearcher 并将我想要的属性添加到 DirectorySearcher.PropertiesToLoad。问题是,这只是确保添加的属性包含在 DirectoryEntry.Properties 以及一些默认列表中。有什么方法可以指定您想要返回的唯一属性吗?
DirectoryEntry base = new DiectoryEntry(rootPath, null, null, AuthenticationTypes.FastBind);
DirectorySearcher groupSearcher = new DirectorySearcher(base);
groupSearcher.Filter = "(objectClass=group)";
groupSearcher.PropertiesToLoad.Add("distinguishedName");
groupSearcher.PropertiesToLoad.Add("description");
foreach (SearchResult groupSr in groupDs.FindAll())
...
在 foreach 循环内,当我获取组 DirectoryEntry 时,我可以访问大约 16 个不同的属性,而不仅仅是我指定的两个(distinguishedName、description)
您限制的内容是将可用/填写在您的属性中SearchResult
对象 - 您可以直接在您的foreach
loop:
DirectoryEntry baseEntry = new DirectoryEntry(rootPath, null, null, AuthenticationTypes.FastBind);
DirectorySearcher groupSearcher = new DirectorySearcher(baseEntry);
groupSearcher.Filter = "(objectClass=group)";
groupSearcher.PropertiesToLoad.Add("distinguishedName");
groupSearcher.PropertiesToLoad.Add("description");
foreach (SearchResult groupSr in groupSearcher.FindAll())
{
if(groupSr.Properties["description"] != null && groupSr.Properties["description"].Count > 0)
{
string description = groupSr.Properties["description"][0].ToString();
}
.....
}
您不能限制实际的属性DirectoryEntry
- 所以如果你去获取每个的目录条目SearchResult
- 您可以完全访问所有内容。但重点是您可以定义所需的属性并访问这些属性directly on the SearchResult
, without必须回到底层DirectoryEntry
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)