2014-02-23 19 views
0

我正在嘗試創建一個接受Active Directory安全組列表並返回用戶是否爲成員(直接或間接)的布爾響應的方法。我使用的Adaxes(基本上擴展ADSI與他們自己的一些功能)。他們有一個對象(IAdmGroup),它爲一個組的所有成員(直接和間接)返回一個byte []數組。如果可以,我想避免使用這種方法,因爲某些組下有非常大的組(10,000多個用戶),如果我可以提供幫助,我不想影響性能。ADSI間接組成員

下面是我的問題的示例: 組1具有組2作爲成員。用戶1是組2的成員。如果我通過我的方法用戶1和組1,我應該得到「真實」。第1組也有第3組。第3組有10,000名成員,我不願意將該組中的所有10,000多名成員集中到一個集合中,並搜索該集合以查看用戶1是否在其中。

我正在使用C#,.Net4.0和WCF。

這裏是我到目前爲止所(我知道這是不是很多)

public Dictionary<string, bool> CheckGroupMembership(List<string> groups, string guid) 
{ 

    var resp = new Dictionary<string, bool>(); 
    foreach (string group in groups) 
    { 
     var user = getIADsUser("Adaxes://<GUID=" + guid + ">"); //gets the IADsUser object 
     var adGroup = GetGroup(group); //Gets IADsGroup 

    } 
} 

回答

1

您可以使用System.DirectoryServices.AccountManagementWindowsPrincipal

PrincipalContext context = new PrincipalContext(ContextType.Domain, "DomainName"); 
UserPrincipal user = UserPrincipal.FindByIdentity(context, guid); 

WindowsPrincipal wpuser = new WindowsPrincipal(new WindowsIdentity(user.UserPrincipalName)); 
bool blIsInRole = wpuser.IsInRole("TheGroupName"); 
if (blIsInRole) 
    Console.WriteLine("IsInRole : Belongs too"); 
else 
    Console.WriteLine("IsInRole : Don't Belongs too"); 
0

而是讓所有組的所有成員,你需要獲得組的用戶是一個成員:

public Dictionary<string, bool> CheckGroupMembership(List<string> groups, string guid) 
{ 
    // Get GUIDs of groups 
    IADsUser user = getIADsUser("Adaxes://<GUID=" + guid + ">"); //gets the IADsUser object 
    Object[] parentGroupGuidsArray = (Object[])user.GetEx("adm-MemberOfGuid"); 
    HashSet<Guid> parentGroupGuids = new HashSet<Guid>(); 
    foreach (Byte[] guidAsBytes in parentGroupGuidsArray) 
    { 
     parentGroupGuids.Add(new Guid(guidAsBytes)); 
    } 

    // Add groups to result dictionary 
    var resp = new Dictionary<string, bool>(groups.Count, StringComparer.OrdinalIgnoreCase); 
    foreach (String group in groups) 
    { 
     IADsGroup adGroup = GetGroup(group); //Gets IADsGroup 
     Guid groupGuid = new Guid((Byte[])adGroup.Get("objectGuid")); 
     resp.Add(group, parentGroupGuids.Contains(groupGuid)); 
    } 

    return resp; 
}