如何列出在DropDownList控件中的Active Directory中的所有用户控件、出在、用户、DropDownList

2023-09-09 21:37:51 作者:踮起脚亲你

我在使用Visual Studio 2005的C#。

I am using Visual Studio 2005 C#.

我试图以获取用户的名单在我的Active Directory和将它们插入到的的DropDownList 的控制。

I am trying to retrieve the list of users in my Active Directory and insert them into a DropDownList control.

我想知道我怎么拉出来的用户,我怎么可以把它们插入到的的DropDownList 的 控制?

May I know how do I pull out the users and how can I insert them into the DropDownList control?

编辑:

有的功能,很多地方我都希望完成。

There are many parts of the functionality I would wish to complete.

首先是列出所有用户在的DropDownList 的,并具有2复选框的用户和管理的和基本分配给角色在用户中的 DDL 的,相应的复选框将被选中。

Firstly is to list all users in a DropDownList, and having 2 checkboxes, User and Admin, and base on the role assigned to the user in the DDL, appropriate checkboxes will be checked.

检查和角色复选框也将指定的取消选中/取消相应的作用。

Checking and unchecking of the role checkboxes will also assign/revoke the roles accordingly.

推荐答案

如果你在.NET 3.5及以上,可以使用 PrincipalSearcher 和查询逐例如主要做你的搜索:

If you're on .NET 3.5 and up, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// 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文章管理目录安全主体在.NET Framework 3.5 这表明很好如何使新功能的最佳使用 System.DirectoryServices.AccountManagement

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

这code可能是相当缓慢的 - 特别是如果你有一个大广告,并在广告中大量的用户。不过话又说回来:是不是真的有帮助,列出成千上万的用户在一个单一的降降?您可能需要重新考虑你的策略有....

This code could be rather slow - especially if you have a large AD, and a large number of users in your AD. But then again: is it really helpful to list thousands of users in a single drop down?? You might need to rethink your strategy there....

更新:如果您不能使用.NET 3.5,你将不得不使用的遗产 DirectorySearcher从类代替 - 这样的事情

Update: if you cannot use .NET 3.5, you'll have to use the "legacy" DirectorySearcher class instead - something like this:

// 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用户退还。)等问题。你也许应该允许用户搜索作为用户,例如:他们的名字什么的 - 而不是列举出的所有你的用户(根据AD的大小)

As I mentioned - this will not perform very well on a large AD, and you might run into limits (like max. of 1'000 users returned) and other issues. You should maybe allow users to search for users, e.g. by their name or something - rather than listing out all your users (depending on the size of your AD).