如何在 Active Directory C# 中的指定 ou 下面列出 ous(组织单位)
由于我在从 Active Directory 检索信息方面经验不是很丰富,因此我希望得到一些正确方向的指导。
我想列出指定 ou 下面的所有 ous(组织单位),但不幸的是我不知道如何让事情正常运行。
假设我们 AD 中的结构如下,
SomeStartingPoint
|-MySpecifiedOuName
|-SubOuName1
|-SubOuName2
到目前为止我得到的是
DirectoryEntry rootDSE = new DirectoryEntry( "LDAP://RootDSE" );
string defaultNamingContext = rootDSE.Proeprties[ "defaultNamingContext" ].Value.ToString();
DirectoryEntry entry = new DirectoryEntry( "LDAP://" + defaultNamingContext );
DirectorySearcher ouSearch =
new DirectorySearcher( entry.Path ) { Filter = "(objectCategory=organizationalUnit)", SearchScope = SearchScope.Subtree };
ouSearch.PropertiesToLoad.Add( "name" );
ouSearch.PropertiesToLoad.Add( "adspath" );
SearchResultCollection allOUs = ouSearch.FindAll();
现在我可以循环 allOUs
并访问 .Properties[ "name" ][ 0 ] 和
.Properties[ "adspath" ][ 0 ]
列出所有 OU 的值。
现在,当我尝试使用 Filter = (&(objectCategory=organizationalUnit)(ou=MySpecifiedOuName))
中的不同过滤器时,我确实得到了与 MySpecifiedOuName
完全对应的单个条目code>,但不是下面的底层 OU,即使它们的路径中包含 MySpecifiedOuName
。这可能是由于在我的示例中我查询了错误的内容(直接查询 OU),但我不知道任何其他方式。
有什么想法吗?
As I'm not very experienced in terms of retrieving information from the Active Directory I hope to get some pointers into the right direction.
I'd like to list all ous (organizational units) below a specified ou, but unfortunately I don't know how to get things working.
Let's say that the structure in our AD is as follows
SomeStartingPoint
|-MySpecifiedOuName
|-SubOuName1
|-SubOuName2
What I've got so far is
DirectoryEntry rootDSE = new DirectoryEntry( "LDAP://RootDSE" );
string defaultNamingContext = rootDSE.Proeprties[ "defaultNamingContext" ].Value.ToString();
DirectoryEntry entry = new DirectoryEntry( "LDAP://" + defaultNamingContext );
DirectorySearcher ouSearch =
new DirectorySearcher( entry.Path ) { Filter = "(objectCategory=organizationalUnit)", SearchScope = SearchScope.Subtree };
ouSearch.PropertiesToLoad.Add( "name" );
ouSearch.PropertiesToLoad.Add( "adspath" );
SearchResultCollection allOUs = ouSearch.FindAll();
Now I can loop over the allOUs
and access .Properties[ "name" ][ 0 ]
and .Properties[ "adspath" ][ 0 ]
to list the values for all OUs.
Now when I try to use a different filter as in Filter = (&(objectCategory=organizationalUnit)(ou=MySpecifiedOuName))
, I do get the single entry corresponding to exactly MySpecifiedOuName
, but not the underlying OUs below, even though they contain MySpecifiedOuName
within their path. This is probably down to the fact that in my example I query the wrong thing (the OU directly), but I don't know any other way.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需尝试使用此过滤器:
通过 RootDSE 的
configurationNamingContext
适应您的域CN=Configuration,DC=dom,DC=fr
这可以通过以下事实来解释:objectCategory是一个独特的名字,我知道微软工具正在进行翻译,但它似乎不适合你。
-----已编辑-----
由于@Desmond 坚持事实
“(objectCategory=organizationalUnit)”
我只是测试它它是否有效。“(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=dom,DC=fr)”
也有效。Just try with this filter :
Adapted to your domain with
configurationNamingContext
from the RootDSE forCN=Configuration,DC=dom,DC=fr
This may be explained by the fact that objectCategory is a distinguich name, I know that Microsoft tools are making the translation but it seems not to work for you here.
-----Edited-----
As @Desmond insist on the fact
"(objectCategory=organizationalUnit)"
I just test it an it works."(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=dom,DC=fr)"
also works.做你想做的事情的唯一方法是以递归方式。
ou
是 OU 的 RDN 属性(与用户的CN
非常相似)。因此,搜索ou=foo
只会得到OU
属性设置为 foo 的 OU。为了遍历该链,您需要搜索当前级别的所有 OU(使用
OneLevel
搜索而不是SubTree
),并且然后在那里递归。但是,这根本没有效率,因为您将向 AD 发出大量查询。相反,您可以做您正在做的事情,然后根据 DN/深度进行排序来构建层次结构。这更复杂,但从资源访问的角度来看,它会更有效。
The only way to do what you want is in a recursive fashion.
ou
is the RDN attribute for the OU (much likeCN
is for users). Thus doing a search forou=foo
will only get you OUs with theirOU
attribute set to foo.In order to walk the chain, you're going to need to do a search for all the OUs at the current level (use a
OneLevel
search instead ofSubTree
), and then recurse through there. This is not at all efficient, though, since you're going to be issuing numerous queries to AD.Instead, you could do what you're doing and then build your hierarchy by sorting based on DN/depth. This is more complex but it will be more efficient from a resource access point of view.
您很可能只缺少
subtree
选项:You most likley are only missing the
subtree
option: