简体   繁体   English

使用DirectorySearcher的C#中的LDAP查询不返回任何内容

[英]LDAP Query in c# using DirectorySearcher does not return anything

I run the following LDAP query using DirectorySearcher which is bound to the root of the AD: 我使用与AD的根绑定的DirectorySearcher运行以下LDAP查询:

(&(|(objectClass=container)(objectClass=organizationalUnit))(!(distinguishedName=OU=)(*&^%\\\#@$!\\\#$%^,OU=xyxy.yxyx,OU=yyyy,OU=tttt,DC=base,DC=local)))

But, it does not return any OUs. 但是,它不返回任何OU。 When i run the same query with an other OU like below, i get all the other OUs in AD: 当我使用另一个如下所示的OU运行相同的查询时,我得到了AD中的所有其他OU:

(&(|(objectClass=container)(objectClass=organizationalUnit))(!(distinguishedName=OU=SOMEOU,OU=xyxy.yxyx,OU=yyyy,OU=tttt,DC=base,DC=local)))

Basically, I am trying to run a query for all the OUs which exclude some specific OUs. 基本上,我试图对所有不包括某些特定OU的OU运行查询。 I guess it has something to do with the special character in the OU name. 我想这与OU名称中的特殊字符有关。

I am assuming the the name of the OU is )(*&^%\\#@$!\\#$%^ as it appears in active directory users and computers. If you open the attribute editor you would find the following value for the distinguishedName attribute: OU=)(*&^%\\\\\\#@$!\\\\\\#$%^,OU=.... (notice that here the number of backslashes doubled and an extra backslash is added). 我假设OU的名称是)(*&^%\\#@$!\\#$%^因为它出现在活动目录用户和计算机中。如果打开属性编辑器,您将找到以下值distinguishedName属性: OU=)(*&^%\\\\\\#@$!\\\\\\#$%^,OU=.... (请注意,此处的反斜杠数量增加了一倍,并添加了一个额外的反斜杠)。

Based on this MSDN page , ( , ) , * , and \\ (and others) need to be escaped. 基于此MSDN页面 ,需要转义()*\\ (及其他)。

  • * becomes \\2a *变成\\ 2a

  • ( becomes \\28 (变成\\ 28

  • ) becomes \\29 )变成\\ 29

  • \\ becomes \\5c \\变成\\ 5c

So your OU name in the LDAP filter should become OU=\\29\\28\\2a&^%\\5c\\5c\\5c#@$!\\5c\\5c\\5c#$%^,OU=.... 因此,您在LDAP过滤器中的OU名称应变为OU=\\29\\28\\2a&^%\\5c\\5c\\5c#@$!\\5c\\5c\\5c#$%^,OU=....

Please note that you need to escape the \\ characters here or use @ in C# to create a verbatim string like this: 请注意,您需要在此处转义\\字符,或在C#中使用@创建这样的逐字字符串:

string dn = @"OU=\29\28\2a&^%\5c\5c\5c#@$!\5c\5c\5c#$%^,OU=...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM