gym*_*ode 6 c# asp.net active-directory visual-studio
我正在使用Visual Studio 2005 C#.
我试图检索我的Active Directory中的用户列表,并将它们插入到DropDownList控件中.
我可以知道如何提取用户以及如何将它们插入DropDownList 控件?
编辑:
我希望完成许多功能部分.
首先列出DropDownList中的所有用户,并有2个复选框,User和Admin,并根据DDL中分配给用户的角色,将检查相应的复选框.
选中和取消选中角色复选框也会相应地分配/撤消角色.
mar*_*c_s 10
如果您使用的是.NET 3.5及更高版本,则可以使用a 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.....
}
Run Code Online (Sandbox Code Playgroud)
如果您还没有 - 绝对阅读MSDN文章.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;
Run Code Online (Sandbox Code Playgroud)
正如我提到的-这不是一个大AD表现非常好,你可能会遇到的限制(如最大的1'000用户退还.)等问题.您应该允许用户搜索用户,例如通过他们的姓名或其他内容 - 而不是列出所有用户(取决于您的AD的大小).