2013-05-10 66 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中的用戶和羣組玩耍!