LDAP查询以返回组中的所有用户

时间:2016-04-18 16:47:30

标签: .net active-directory ldap directoryservices

我正在编写一个LDAP接口,对于给定的组objectguid,必须返回这些组中所有用户的列表以及用户的SID。

对于给定的群组objectguid,以下代码会返回该群组中的用户。但是它们都是......

CN=Chad Hutchins,OU=Contractors,DC=RM,DC=LOCAL

...但我需要用户的SID与上面的字符串相对应。有没有办法从组中获取用户的SID并查询该组中的所有用户?

using System.DirectoryServices;

public void GetUsers(Guid groupId, string domain, string username, string password)
{
    var rootEntry = new DirectoryEntry("LDAP://" + domain);
    rootEntry.Username = username;
    rootEntry.Password = password;

    var searcher = new DirectorySearcher(rootEntry);
    searcher.Filter = @"(objectguid=" + ConvertGuidToOctectString(groupId) + ")";

    var groupResult = searcher.FindOne();
    foreach (DictionaryEntry prop in groupResult.Properties)
    {
        var key = (string)prop.Key;

        switch (key)
        {
            case "member":
                foreach (string name in groupResult.Properties[key])
                    Console.WriteLine(name);
                break;
        }
    }
}

private string ConvertGuidToOctectString(Guid guid)
{
    var byteGuid = guid.ToByteArray();
    var queryGuid = string.Empty;
    foreach (var b in byteGuid)
    {
        queryGuid += @"\" + b.ToString("x2");
    }
    return queryGuid;
}

1 个答案:

答案 0 :(得分:1)

关于如何从特定SIDs检索用户group的选项很少。

  1. GroupPrincipal namepsace中使用AccountManagement

    public static List<string> GetUsersFromGroupByGroupID(string ID)
    {
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com")
        {
            using (GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Guid, ID))
            {
                if (group != null)
                {
                    List<string> memberSIDs = new List<string>();
                    var members = group.GetMembers(true);
                    foreach(var member in members)
                    {
                        memberSIDs.Add(member.Sid.ToString());
                    }
                    return memberSIDs;
                }
            }
        }
        return null;
    }
    
  2. 您可以将查询中所有用户的DistinguishedName存储到List<string>,然后使用SID课程查找用户UserPrincipal

    public static List<string> GetUserSIDs(List<string>userDNs)
    {
        List<string> userSIDs = new List<string>();
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
        {
            foreach(string userDN in userDNs)
            {
                using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, userDN))
                {
                    if (user != null)
                    {
                        userSIDs.Add(user.Sid.ToString());
                    }
                }
            }
        }
        return userSIDs;
    }
    
  3. 最后一个选项仍然可以从您的查询中获取DistiniguishedName列表并仍使用DirectoryEntry

    using (DirectoryEntry entry = new DirectoryEntry("LDAP://userDistinguishedName")
    {
        var userSID = entry.Properties["objectSID"][0];
    }
    
  4. 注意*在这种情况下,userSID将返回byte[] array