我正在使用Intranet ASP.NET MVC Web应用程序,并且需要从Active Directory检索所有用户名(我不需要组)。我如何检索Active Directory用户列表
目前我已启用Windows身份验证,用户可以直接登录到Web应用程序。而对于数据访问层,我使用的是实体框架。
但我需要获取用于管理目的的所有用户名,并将这些用户分配给我创建的应用程序自定义组。有关如何完成此任何想法?
我正在使用Intranet ASP.NET MVC Web应用程序,并且需要从Active Directory检索所有用户名(我不需要组)。我如何检索Active Directory用户列表
目前我已启用Windows身份验证,用户可以直接登录到Web应用程序。而对于数据访问层,我使用的是实体框架。
但我需要获取用于管理目的的所有用户名,并将这些用户分配给我创建的应用程序自定义组。有关如何完成此任何想法?
您可以使用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文章Managing Directory Security Principals in the .NET Framework 3.5这很好地说明如何使System.DirectoryServices.AccountManagement
中的新功能的最佳使用。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空间。
当然,这取决于你的需要,你可能想在你创建一个“查询通过例如”用户主体指定其他属性:
DisplayName
(通常为:第一名称+空格+姓氏)SAM Account Name
- 你的Windows/AD帐户名User Principal Name
- 你的 “[email protected]” 样式名可以SPE将UserPrincipal
上的任何属性都作为属性,并将它们用作您的PrincipalSearcher
的“查询范例”。
感谢您的答复,我在模型文件夹内创建了一个新类。但是我收到了以下错误,无法找到PrincipalContext&ContextType.Domain。 –
@johnG:您需要为您的项目添加对“System.DirectoryServices.AccountManagement”的引用 –