(&(objectClass = user)(||(displayName))搜索过滤器无效

时间:2018-06-22 03:15:22

标签: c# asp.net active-directory ldap

Error:Internal:无法执行代码阶段,因为代码阶段抛出了异常:(&(objectClass = user)(|(displayName))搜索过滤器无效。

emailAddress = "";
displayName = "Smith";

DirectorySearcher oSearch = new DirectorySearcher();
//oSearch.Filter = "name=" + displayName;
//oSearch.Filter = "(&(objectClass=user)(|(sAMAccountName = displayName))";
oSearch.Filter = "(&(objectClass=user)(|(displayName))";
SearchResult oResult = oSearch.FindOne();
DirectoryEntry oEntry = oResult.GetDirectoryEntry();

emailAddress = oEntry.Properties["emailAddress"].Value.ToString();

尝试根据显示名称获取电子邮件地址。

也尝试过这些。

(&(objectClass=user)(&(displayName = {Smith})) 


oSearch.Filter = string.Format("(&(objectClass=user)(&(displayName = {Smith})))", displayName);

谢谢!

1 个答案:

答案 0 :(得分:1)

由于您错误地使用OR(|)运算符,因此搜索过滤器无效/不正确。

尽管问题中没有给出任何条件,但我假设您要查找显示名称为“ Smith”的用户的电子邮件地址。

在这种情况下,您的过滤器为:

oSearch.Filter = "(&(objectClass=user)(displayName=Smith))";
  // check the placement of brackets and the operator carefully as shown above.
  // if you need to search for names starting with "Smith", use wildcard: (displayName=Smith*)
SearchResult oResult = oSearch.FindOne();

if (null != oResult) // check for null values everywhere in your code to avoid NPE
DirectoryEntry oEntry = oResult.GetDirectoryEntry();

if (null != oEntry) // check for null values, else NP exception will be thrown
emailAddress = oEntry.Properties["emailAddress"].Value.ToString();

请参阅TechNet文章Active Directory: LDAP Syntax Filters,以获取更多信息。