C# 使用包含空格的LDAP筛选器查询Active Directory
我试图通过LDAP查询ActiveDirectory,但查询可能包含空格或其他可能导致问题的字符(连字符?) 它是一个OR搜索,例如,在SQL中,它应该是C# 使用包含空格的LDAP筛选器查询Active Directory,c#,active-directory,C#,Active Directory,我试图通过LDAP查询ActiveDirectory,但查询可能包含空格或其他可能导致问题的字符(连字符?) 它是一个OR搜索,例如,在SQL中,它应该是WHERE(sn像'Bloggs%'和givenName像'Jo%')或displayName像'Jo Bloggs' 但是,当我尝试LDAP查询时,我得到一个错误:System.ArgumentException:(&(objectCategory=person)(objectClass=user)(|)(&(sn=Bloggs*)(give
WHERE(sn像'Bloggs%'和givenName像'Jo%')或displayName像'Jo Bloggs'
但是,当我尝试LDAP查询时,我得到一个错误:System.ArgumentException:(&(objectCategory=person)(objectClass=user)(|)(&(sn=Bloggs*)(givenName=Jo*)(displayName=Jo Bloggs))搜索筛选器无效
执行搜索的代码:
string userName = "Jo Bloggs";
DirectoryEntry adroot = new DirectoryEntry("LDAP://" + Environment.UserDomainName, "user", "password", AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher(adroot);
search.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(|(&(sn={0}*)(givenName={1}*))(displayName={2}))", userName.Split(' ')[1], userName.Split(' ')[0], userName);
这只是一个基本的搜索,我还想搜索其他列(职位、电话、部门等),例如WHERE Title像“%foo%”或telephonenumber像“%foo%”或Department像“%foo%”
另外,我是否可以缓存搜索,这样ActiveDirectory就不会从搜索相同内容的人那里获得很多点击率
这也只会找到一个条目,我希望在中继器中搜索并显示找到的所有结果。缺少右括号。请尝试以下工作示例:
string userName = "Jo Bloggs";
string baseQuery =
"(&" +
"(objectCategory=person)" +
"(objectClass=user)" +
"(|" +
"(&" +
"(sn={0}*)" +
"(givenName={1}*)" +
")" +
"(displayName={2})" +
")" +
")"; // <<< this is missing in your original query
userName = Regex.Replace(userName, @"[\(\)\*\\]", (match) =>
{ // escape reserved chars
return "\\" + ((int)match.Value[0]).ToString("x");
}, RegexOptions.Compiled);
string query = String.Format(query, userName.Split(' ')[1],
userName.Split(' ')[0], userName);
using (DirectoryEntry entry = new DirectoryEntry(
"LDAP://" + Environment.UserDomainName, "user", "password",
AuthenticationTypes.Secure))
{
using (DirectorySearcher ds =
new DirectorySearcher(entry, query, null, SearchScope.Subtree))
{
SearchResultCollection res = ds.FindAll(); // all matches
if (res != null)
foreach (SearchResult r in res)
Console.WriteLine(user.Properties["displayName"].Value);
}
}
string userName=“Jo Bloggs”;
字符串基查询=
"(&" +
“(对象类别=人)+
“(objectClass=用户)+
"(|" +
"(&" +
“(sn={0}*)”+
“(givenName={1}*)”+
")" +
“(显示名称={2})”+
")" +
“”“/>您缺少一个右括号。请尝试以下工作示例:
string userName = "Jo Bloggs";
string baseQuery =
"(&" +
"(objectCategory=person)" +
"(objectClass=user)" +
"(|" +
"(&" +
"(sn={0}*)" +
"(givenName={1}*)" +
")" +
"(displayName={2})" +
")" +
")"; // <<< this is missing in your original query
userName = Regex.Replace(userName, @"[\(\)\*\\]", (match) =>
{ // escape reserved chars
return "\\" + ((int)match.Value[0]).ToString("x");
}, RegexOptions.Compiled);
string query = String.Format(query, userName.Split(' ')[1],
userName.Split(' ')[0], userName);
using (DirectoryEntry entry = new DirectoryEntry(
"LDAP://" + Environment.UserDomainName, "user", "password",
AuthenticationTypes.Secure))
{
using (DirectorySearcher ds =
new DirectorySearcher(entry, query, null, SearchScope.Subtree))
{
SearchResultCollection res = ds.FindAll(); // all matches
if (res != null)
foreach (SearchResult r in res)
Console.WriteLine(user.Properties["displayName"].Value);
}
}
string userName=“Jo Bloggs”;
字符串基查询=
"(&" +
“(对象类别=人)+
“(objectClass=用户)+
"(|" +
"(&" +
“(sn={0}*)”+
“(givenName={1}*)”+
")" +
“(显示名称={2})”+
")" +
“;”;/为什么不使用Linq to AD进行搜索-为什么不使用Linq to AD进行搜索-还有另一个Linq to AD框架,它实现了更多的IQueryable扩展方法(Single、First、Last、SingleOrDefault等)还有另一个LINQ-to-AD框架,它实现了更多的IQueryable扩展方法(Single、First、Last、SingleOrDefault和更多),并且具有频繁的更新。缺少括号是代码不起作用的原因。.按您的方式编写(分解成行),使其更易于理解和调试如果displayName包含括号,例如“John Smith(PA to Director)”或部门有&-“Finance&Performance”?请注意,缺少括号是代码不起作用的原因。按您的方式编写(分成几行),使其更易于理解和调试如果displayName包含括号,例如“John Smith(PA to Director)”或部门有&-“Finance&Performance”?修复了这一问题,请现在查看并接受我以前问题的更多答案。但是,有一些问题没有答案(或者没有答案)(例如,无法完成,系统限制),或答案没有完全回答问题)现在接受了我以前问题的更多答案。但是,有一些没有回答问题(或者没有答案(例如,无法完成,系统限制),或者答案没有完全回答问题)
ASCII Escape sequence
character substitute
* "\2a"
( "\28"
) "\29"
\ "\5c"
NUL "\00"