2011-12-14 136 views
3

假設用戶johnsmith是活動目錄組MyManagers的成員。 假設組MyManagers是MyEmployees組的成員。 假設組MyEmployees是MyUsers組的成員。如何檢查Active Directory組是否是另一個Active Directory組的成員

當johnsmith登錄到我的應用程序時,我如何知道他是MyUsers組的成員?

欣賞C#中的示例。

感謝, kruvi

+0

你試過了什麼? – 2011-12-14 14:41:42

回答

4

如果你在.NET 3.5及以上,你應該看看System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find a user 
UserPrincipal user = UserPrincipal.Current; // this would be John Smith 

if(user != null) 
{ 
    // get the user's groups he's a member of 
    PrincipalSearchResult<Principal> results = user.GetAuthorizationGroups(); 

    // now you just need to iterate over the groups and see if you find the 
    // one group you're interested in 
} 

GetAuthorizationGroups S.DS.AM中的調用確實進行了遞歸查詢,例如它也會選擇你的用戶所屬的任何羣組,因爲羣組是其他羣組的成員。

新的S.DS.AM可以很容易地與AD中的用戶和羣組玩耍!