.net 如何检索Active Directory用户列表
我正在开发一个intranet ASP.NET MVC web应用程序,我需要从Active Directory检索所有用户名(我不需要组) 目前,我已启用Windows身份验证,用户可以直接登录到web应用程序。对于数据访问层,我使用实体框架.net 如何检索Active Directory用户列表,.net,asp.net-mvc,active-directory,.net,Asp.net Mvc,Active Directory,我正在开发一个intranet ASP.NET MVC web应用程序,我需要从Active Directory检索所有用户名(我不需要组) 目前,我已启用Windows身份验证,用户可以直接登录到web应用程序。对于数据访问层,我使用实体框架 但是为了便于管理,我需要获取所有用户名,并将这些用户分配给我创建的应用程序自定义组。您知道如何实现这一点吗?您可以使用PrincipalSearcher和“示例查询”主体进行搜索: // create your domain context using
但是为了便于管理,我需要获取所有用户名,并将这些用户分配给我创建的应用程序自定义组。您知道如何实现这一点吗?您可以使用
PrincipalSearcher
和“示例查询”主体进行搜索:
// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
// 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
中的新功能。或者查看名称空间
当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:
(通常为:名字+空格+姓氏)DisplayName
-您的Windows/AD帐户名SAM帐户名
-您的”username@yourcompany.com“样式名用户主体名称
您可以在
UserPrincipal
上指定任何属性,并将其用作PrincipalSearcher
的“示例查询”,谢谢您的回复,我在model文件夹中创建了一个新类。但我发现以下错误,PrincipalContext&ContextType.Domain找不到。@johnG:您需要在项目中添加对System.DirectoryServices.AccountManagement
的引用