2013-05-10 61 views
2

我知道如何使用User.IdentityUser.IsInRole活动目录 - 用户的角色

有没有办法看到所有的角色的用户是吗?

我们有很多团体和一些人在很多团体,但我不想写一个User.IsInRole 20+次。

回答

5

在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 
     } 
    } 
} 

的新的S.DS.AM可以很容易地与AD中的用户和群组玩耍!