【问题标题】:How to list all users in Active Directory in DropDownList Control如何在 DropDownList 控件中列出 Active Directory 中的所有用户
【发布时间】:2012-01-18 07:00:40
【问题描述】:

我正在使用 Visual Studio 2005 C#。

我正在尝试检索我的 Active Directory 中的用户列表并将它们插入到 DropDownList 控件中。

我可以知道如何拉出用户以及如何将它们插入到 DropDownList 控制?


编辑:

我希望完成功能的许多部分。

首先是列出 DropDownList 中的所有用户,并有 2 个复选框,User 和 Admin,并基于分配给的角色DDL 中的用户,将选中相应的复选框。

选中和取消选中角色复选框也会相应地分配/撤销角色。

【问题讨论】:

  • 标准是什么?您是否从某事开始并卡住了,请在同一位置展开帖子。开始阅读可能会有所帮助Almost everything in AD

标签: c# asp.net visual-studio active-directory


【解决方案1】:

如果您使用 .NET 3.5 及更高版本,则可以使用 PrincipalSearcher 和“按示例查询”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for all users
UserPrincipal qbeUser = new UserPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果您还没有 - 一定要阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能

这段代码可能会相当慢 - 特别是如果您有一个很大的 AD,并且您的 AD 中有大量用户。但话又说回来:在一个下拉列表中列出数千个用户真的有帮助吗?您可能需要重新考虑那里的策略....

更新:如果您不能使用 .NET 3.5,则必须改用“旧版”DirectorySearcher 类 - 如下所示:

// find your default naming context
DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
string defaultCtx = deRoot.Properties["defaultNamingContext"].Value.ToString();

// define a directory searcher for your default context
string searchRootLDAPPath = "LDAP://" + defaultCtx;
DirectoryEntry defaultDE = new DirectoryEntry(searchRootLDAPPath);

// define searcher - search through entire subtree, search for users 
// (objectCategory=Person)           
DirectorySearcher dsAllUsers = new DirectorySearcher(defaultDE);
dsAllUsers.SearchScope = SearchScope.Subtree;
dsAllUsers.Filter = "(objectCategory=Person)";

// get the results
SearchResultCollection result = dsAllUsers.FindAll();

// count the user objects found
int count = result.Count;

正如我所提到的 - 这不会在大型广告上表现得很好,并且您可能会遇到限制(例如最多返回 1'000 个用户)和其他问题。您可能应该允许用户搜索用户,例如通过他们的名字或其他名称 - 而不是列出所有您的用户(取决于您的广告的大小)。

【讨论】:

  • 感谢您的解决方案。但是我使用的是 .NET 2.0,它还能工作吗?除了通过 DDL 列出之外,还有哪些其他可能的解决方案?
  • @RUiHAO:不,这仅适用于 .NET 3.5 及更高版本,抱歉。另一种选择是允许搜索用户(而不是抓取所有成千上万的用户并显示他们......)
【解决方案2】:

就像 marc_s 的回答一样,我的回答也取决于 .Net 3.5。但只是因为它使用了 LINQ,它可以被翻译回一些 foreach 循环,使得代码也可以在 .Net 2.0 下运行。

但由于我是 DRY 的粉丝,这里只是 the link to an old answer of myself。

【讨论】:

    【解决方案3】:

    对于当前域,

    DirectorySearcher AllUsers = new DirectorySearcher(Domain.GetCurrentDomain().GetDirectoryEntry());
    AllUsers.SearchScope = SearchScope.Subtree;
    AllUsers.Filter = "(&(objectClass=user)(objectCategory=person))";
    int count = AllUsers.FindAll().Count;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多