如何在 Active Directory C# 中的指定 ou 下面列出 ous(组织单位)

发布于 2024-12-08 15:37:18 字数 1269 浏览 0 评论 0原文

由于我在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老街孤人 2024-12-15 15:37:18

只需尝试使用此过滤器:

"(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=dom,DC=fr)"

通过 RootDSE 的 configurationNamingContext 适应您的域 CN=Configuration,DC=dom,DC=fr

这可以通过以下事实来解释:objectCategory是一个独特的名字,我知道微软工具正在进行翻译,但它似乎不适合你。

-----已编辑-----

由于@Desmond 坚持事实“(objectCategory=organizationalUnit)” 我只是测试它它是否有效。 “(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=dom,DC=fr)” 也有效。

DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr", "jpb", "Pwd");

/* Directory Search
 */
DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
dsLookForOUs.SearchScope = SearchScope.Subtree;
dsLookForOUs.PropertiesToLoad.Add("cn");
dsLookForOUs.PropertiesToLoad.Add("ou");

SearchResultCollection srcOUs = dsLookForOUs.FindAll();

foreach (SearchResult srOU in srcOUs)
{
  Console.WriteLine("{0}", srOU.Path);
}

Just try with this filter :

"(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=dom,DC=fr)"

Adapted to your domain with configurationNamingContext from the RootDSE for CN=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.

DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr", "jpb", "Pwd");

/* Directory Search
 */
DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
dsLookForOUs.SearchScope = SearchScope.Subtree;
dsLookForOUs.PropertiesToLoad.Add("cn");
dsLookForOUs.PropertiesToLoad.Add("ou");

SearchResultCollection srcOUs = dsLookForOUs.FindAll();

foreach (SearchResult srOU in srcOUs)
{
  Console.WriteLine("{0}", srOU.Path);
}
寄风 2024-12-15 15:37:18

做你想做的事情的唯一方法是以递归方式。 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 like CN is for users). Thus doing a search for ou=foo will only get you OUs with their OU 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 of SubTree), 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.

思念绕指尖 2024-12-15 15:37:18

您很可能只缺少 subtree 选项:

ouSearch.SearchScope = SearchScope.Subtree;

You most likley are only missing the subtree option:

ouSearch.SearchScope = SearchScope.Subtree;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文