Active Directory - 用户的角色

Jam*_*son 2 c# active-directory

我理解如何使用User.IdentityUser.IsInRole

有没有办法查看用户所在的所有角色?

我们有很多小组,有些人在很多小组,但我不想写User.IsInRole 20多次.

mar*_*c_s 8

在Active Directory上下文中,您引用的角色实际上是用户所属的安全(或授权)组.

因此,如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间.在这里阅读所有相关内容:

基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // find a user
   UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

   if(user != null)
   {
       // get the authorization groups - those are the "roles" 
       var groups = user.GetAuthorizationGroups();

       foreach(Principal principal in groups)
       {
           // do something with the group (or role) in question
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!