C# Active Directory-用户的角色

C# Active Directory-用户的角色,c#,active-directory,C#,Active Directory,我了解如何使用User.Identity和User.IsInRole 是否有办法查看用户所处的所有角色 我们有很多组,有些人在很多组中,但我不想写一个User.IsInRole 20多次。在Active Directory上下文中,您所指的角色实际上是用户所属的安全(或授权)组 因此,如果您使用的是.NET3.5及更高版本,那么应该检查System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。请在此处阅读所有相关内容: 基本上,您可以

我了解如何使用
User.Identity
User.IsInRole

是否有办法查看用户所处的所有角色


我们有很多组,有些人在很多组中,但我不想写一个User.IsInRole 20多次。

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

因此,如果您使用的是.NET3.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
       }
   }
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易