0

我想在我的应用程序仪表板上显示一个活跃用户列表。

我的所有用户都是员工,并通过他们的 Active Directory 凭据访问应用程序。

我已经使用 UserPrincipal 来获取当前用户的详细信息,但是可以为所有当前用户执行此操作吗?

4

1 回答 1

2

您可以使用 aPrincipalSearcher和“按示例查询”主体进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // define a "query-by-example" principal - here, we search for all "enabled" UserPrincipal 
   UserPrincipal qbeUser = new UserPrincipal(ctx);
   qbeUser.IsEnabled = true;

   // 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,它很好地展示了如何充分利用 .NET Framework 中的新功能System.DirectoryServices.AccountManagement。或查看System.DirectoryServices.AccountManagement命名空间上的 MSDN 文档。

于 2013-04-08T15:21:45.830 回答