【问题标题】:List all Organizational Units (OU) and Sub OU's in aspx page在 aspx 页面中列出所有组织单位 (OU) 和子 OU
【发布时间】:2013-06-25 16:42:06
【问题描述】:

我有一个问题,我一直在寻找答案,但找不到合适的答案。我有一个 ASP.Net Web 应用程序,我需要列出特定 OU 的所有子 OU。有谁现在最好怎么做?

例如,我想列出 Users 下的所有 OU,然后让用户单击该 OU,然后查看该 OU 中包含的用户列表。我已经能够列出 OU 中的用户,但我目前无法列出子 OU。

这是我已经拥有的代码;

DirectoryEntry Ldap = new DirectoryEntry("LDAP://ou=Users;ou=ASH;ou=Establishments;dc=domain;dc=com", aduser, adpass);
DirectorySearcher searcher = new DirectorySearcher(Ldap);
//specify that you search user only by filtering AD objects
searcher.Filter = "(objectClass=user)";

try
{
   foreach (SearchResult result in searcher.FindAll())
   {
      //loop through each object and display the data in a table
      DirectoryEntry DirEntry = result.GetDirectoryEntry();

      TableRow tblRow = new TableRow();
      TableCell tblcell_Username = new TableCell();
      TableCell tblcell_displayName = new TableCell();
      tblcell_Username.Text = DirEntry.Properties["SAMAccountName"].Value.ToString();
      tblcell_displayName.Text = "";
      tblRow.Controls.Add(tblcell_Username);
      tblRow.Controls.Add(tblcell_displayName);
      ADWeb_Tbl.Rows.Add(tblRow);

      //DEBUG LINES
      //On peut maintenant afficher les informations désirées
      //Response.Write("Login: " + DirEntry.Properties["SAMAccountName"].Value);
   }
}
catch (Exception ex)
{
   Response.Write(ex.Source + "<br />");
   Response.Write(ex.Message + "<br />");
   Response.Write(ex.InnerException);
}

有人有什么建议吗?

感谢您抽出宝贵时间阅读此问题。

【问题讨论】:

  • 那么当你运行你的代码时会发生什么?

标签: c# active-directory ldap


【解决方案1】:

两个要点:

  1. 如果您想查找组织单位 - 为什么要搜索用户?!?!?这根本没有意义。使用此代码:

    DirectorySearcher searcher = new DirectorySearcher(Ldap);
    // specify that you search for organizational units 
    searcher.Filter = "(objectCategory=organizationalUnit)";
    searcher.SearchScope = SearchScope.SubTree;  // search entire subtree from here on down
    
  2. 当你从搜索者那里得到结果时,你应该尽量避免在每一个结果上都调用.GetDirectoryEntry()。在DirectorySearcher 中指定您需要的属性 - 然后直接在搜索结果中使用这些属性:

    searcher.PropertiesToLoad.Add("sAMAccountName");  // and add any others you need
    
    try
    {
       foreach (SearchResult result in searcher.FindAll())
       {
          TableRow tblRow = new TableRow();
          TableCell tblcell_Username = new TableCell();
          tblcell_Username.Text = result.Properties["SAMAccountName"].ToString();
    

【讨论】:

  • 我发布的代码只是我目前用于从 AD 获取用户列表的示例。我不认为 SMSAccountName 应用于 OU 的?此外,当我运行此代码时,我收到以下错误:对象引用未设置为对象的实例。有什么想法吗?
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2014-06-28
  • 1970-01-01
  • 2023-01-09
相关资源
最近更新 更多