下面是我执行AD搜索的实现。
DirectoryEntry de = GetDirectoryEntry();
using (var Search = new DirectorySearcher(de))
{
Search.Filter = "....";
Search.PropertiesToLoad.Add("Name");
Search.PropertiesToLoad.Add("distinguishedName");
Search.PropertiesToLoad.Add("objectGUID");
var results = Search.FindAll();
//read properties here...
DirectoryEntry resultde = result.GetDirectoryEntry();
string schClassName = resultde.SchemaClassName; //want to add SchemaClassName to PropertiesToLoad
}我已经能够将name、distinguishedName和objectGUID添加到DirectorySearcher的PropertiesToLoad中。我还想读取SchemaClassName的值。现在我必须调用GetDirectoryEntry方法来读取它。是否可以将DirectorySearcher.SchemaClassName添加到PropertiesToLoad中而不是调用GetDirectoryEntry方法?还有其他更好的选择吗?
发布于 2022-08-09 13:27:13
您正在寻找objectClass属性。
但是,该属性是一个多值属性,它不仅包含对象的类,还包含超类。因此,用户对象将包含值top、person、organizationalPerson和user。
因此,如果要查找对象中最特定的类,请查看最后一个值。例如:
result.Properties["objectClass"][result.Properties["objectClass"].Count - 1]更多阅读:对象类和对象类别
另外,请确保将Search.FindAll()放在using语句中。在文件备注一节中,它说:
由于实现限制,当垃圾回收时,SearchResultCollection类无法释放其所有非托管资源。若要防止内存泄漏,必须在不再需要SearchResultCollection对象时调用Dispose方法。
因此,处理这一点比处理DirectorySearcher更重要。
https://stackoverflow.com/questions/73283526
复制相似问题