Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DropDownList控件中列出Active Directory中的所有用户_C#_Asp.net_Visual Studio_Active Directory - Fatal编程技术网

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

C# 如何在DropDownList控件中列出Active Directory中的所有用户,c#,asp.net,visual-studio,active-directory,C#,Asp.net,Visual Studio,Active Directory,我正在使用VisualStudio2005C 我正在尝试检索Active Directory中的用户列表,并将其插入DropDownList控件 我可以知道如何提取用户以及如何将其插入DropDownList吗 控制 编辑: 我希望完成该功能的许多部分 首先是在DropDownList中列出所有用户,并有2个复选框,User和Admin,并且根据DDL中分配给用户的角色,将选中相应的复选框 选中和取消选中角色复选框也会相应地分配/撤销角色。如果您使用的是.NET 3.5及更高版本,则可以使用P

我正在使用VisualStudio2005C

我正在尝试检索Active Directory中的用户列表,并将其插入DropDownList控件

我可以知道如何提取用户以及如何将其插入DropDownList吗 控制


编辑:

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

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


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

如果您使用的是.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文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement中的新功能

这段代码可能相当慢——特别是如果你有一个大广告,并且广告中有大量的用户。但话说回来:在一个下拉列表中列出数千个用户真的有用吗??你可能需要重新考虑你在那里的战略

更新:如果无法使用.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;

正如我所提到的,这在一个大广告上不会表现得很好,你可能会遇到限制(比如最多返回1000个用户)和其他问题。你也许应该允许用户搜索用户,例如按他们的名字或其他什么-而不是列出你的所有用户(取决于你的广告大小)。

像marc_的答案一样,我的答案也依赖于.Net 3.5。但这只是因为它使用LINQ,可以将LINQ转换回一些foreach循环,所以代码也可以在.NET2.0下运行

但由于我是DRY的粉丝,这里的问题很简单。

对于当前领域

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

标准是什么?如果你刚开始的时候有什么东西卡住了,请在同一张纸上展开这篇文章。开始阅读本书可能会有所帮助,感谢您提供的解决方案。但是我使用的是.NET2.0,它还能工作吗?除了用DDL列出之外,还有哪些可能的解决方案?@RUiHAO:没有,这只适用于.NET 3.5及以上版本,对不起。另一个选项是允许搜索用户(而不是抓取成千上万的用户并显示他们……)