C# 向LDAP搜索添加多个条件
我有一个功能示例代码,用于查询与某个部门编号匹配的所有用户,并为每个用户回显所有关联的LDAP属性:C# 向LDAP搜索添加多个条件,c#,.net,ldap,C#,.net,Ldap,我有一个功能示例代码,用于查询与某个部门编号匹配的所有用户,并为每个用户回显所有关联的LDAP属性: string attribute = "departmentnumber"; string value = "123"; DirectoryEntry rootEntry = new DirectoryEntry("LDAP://ldap.acme.com:389"); rootEntry.AuthenticationType = AuthenticationTypes.None; Dire
string attribute = "departmentnumber";
string value = "123";
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://ldap.acme.com:389");
rootEntry.AuthenticationType = AuthenticationTypes.None;
DirectorySearcher searcher = new DirectorySearcher(rootEntry, $"({attribute}={value})");
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in searcher.FindAll())
{
var allLDAPProperties = result.Properties.PropertyNames;
foreach (var property in allLDAPProperties)
{
Console.WriteLine((result.Properties[property.ToString()].Count > 0 ? result.Properties[property.ToString()][0] : string.Empty).ToString());
}
Console.WriteLine(Environment.NewLine);
}
Console.ReadKey();
但是,我想在我的初始搜索中添加多个条件(例如,返回所有用户的“departmentnumber”属性与“123”匹配,而“joblevel”属性与“5”匹配)。我找不到向DirectorySearcher
提供复合筛选条件的正确方法
我知道我可以通过
foreach
循环中的附加处理来过滤初始结果集,但我正在努力提高效率。为了为LDAP搜索指定多个搜索条件,您需要使用(在链接文档中详细描述)
基本上,如果您需要两个条件,您的搜索过滤器将类似于:
(&(departmentnumber=123)(joblevel=5))
查看链接文档以获取完整解释和更多示例