如何获取域用户SID,它是本地系统组的一部分

时间:2019-12-28 06:48:48

标签: c# directory

我在域中(无工作组)中有一台计算机,并且我有一个本地组Test Users,并且在该组中我添加了域用户。

domain \ Administrator

域\安装

enter image description here

现在,我想从该组中提取所有用户以及他们的SID。使用下面的代码,我可以获取该组中的所有用户名,但是如何获取SID

using (var groupEntry = new DirectoryEntry("WinNT://./Test Users,group"))
            {
                foreach (var member in (IEnumerable)groupEntry.Invoke("Members"))
                {
                    using (var memberEntry = new DirectoryEntry(member))
                    {
                        Console.WriteLine(memberEntry.Name);
                    }
                }
            }

2 个答案:

答案 0 :(得分:1)

要获取DirectoryEntry的SID,必须获取用户/组的扩展属性。您可以通过在DirectoryEntry上调用.Properties获得这些属性。您可以使用["propertyName"].Value来访问每个属性,但是对于SID,必须将SID的字节数组转换为字符串。下面是来自codeproject网站的convert方法以及如何使用它。

将Byte转换为String的方法是taken from here

    private static string ConvertByteToStringSid(Byte[] sidBytes)
        {
            StringBuilder strSid = new StringBuilder();
            strSid.Append("S-");
            try
            {
                // Add SID revision.
                strSid.Append(sidBytes[0].ToString());
                // Next six bytes are SID authority value.
                if (sidBytes[6] != 0 || sidBytes[5] != 0)
                {
                    string strAuth = String.Format
                        ("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
                        (Int16)sidBytes[1],
                        (Int16)sidBytes[2],
                        (Int16)sidBytes[3],
                        (Int16)sidBytes[4],
                        (Int16)sidBytes[5],
                        (Int16)sidBytes[6]);
                    strSid.Append("-");
                    strSid.Append(strAuth);
                }
                else
                {
                    Int64 iVal = (Int32)(sidBytes[1]) +
                        (Int32)(sidBytes[2] << 8) +
                        (Int32)(sidBytes[3] << 16) +
                        (Int32)(sidBytes[4] << 24);
                    strSid.Append("-");
                    strSid.Append(iVal.ToString());
                }

                // Get sub authority count...
                int iSubCount = Convert.ToInt32(sidBytes[7]);
                int idxAuth = 0;
                for (int i = 0; i < iSubCount; i++)
                {
                    idxAuth = 8 + i * 4;
                    UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
                    strSid.Append("-");
                    strSid.Append(iSubAuth.ToString());
                }
            }
            catch (Exception ex)
            {

                return "";
            }
            return strSid.ToString();
        }

这是您使用该方法并获取DirectoryEntry实体的SID的方式。

    var coll = memberEntry.Properties;
    object obVal = coll["objectSid"].Value;
    object userSID;
    if (null != obVal)
    {
        userSID = ConvertByteToStringSid((Byte[])obVal);
    }

答案 1 :(得分:1)

替代解决方案

您问是否还有另一种方法,为此,我正在发布一个用于查找帐户SID的不同过程。使用软件包管理器获取nuget软件包:System.DirectoryServices.AccountManagement

自从MS与System.DirectoryServices.AccountManagement一起问世以来,我已经对该程序集进行了几乎所有AD工作的编码。以下是我编写的代码,用于查找系统本地组中所有组/帐户的所有成员的SID的不同方式。

    // Recursively looks up all members and returns All the SIDs of all 'User' or 'local' accounts. 
    // ---> (NOT GROUPS but you can change that if you'd like.)
    private static List<string> GetSidsForAllAccounts(GroupPrincipal grp)
    {
        List<string> listOfSids = new List<string>();
        foreach (var member in grp.Members)
        {
            if (member.StructuralObjectClass != null && member.StructuralObjectClass.ToLower().Equals("group"))
                listOfSids.AddRange(GetSidsForAllAccounts((GroupPrincipal)member));
            else
                listOfSids.Add(member.Sid.ToString());
                // You could also use below to get Name and SID, PIPE delimited.
                // listOfSids.Add($"{member.Name}|{member.Sid.ToString()}");
                // You'll have to cast member to UserPrincipal if you are looking for properties specific to User Account.
        }
        return listOfSids;
    }

在主要方法中使用:您可以通过以下方式调用上述方法。

    // Look up the definition of PrincipalContext to use credentials.
    // e.g. new PrincipalContext(ContextType.Machine, "domainName", "user", "pass");
    PrincipalContext local = new PrincipalContext(ContextType.Machine);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(local, "Administrators");

    List<string> allSids = GetSidsForAllAccounts(grp);
    allSids.ForEach(x => Console.WriteLine(x));

工作原理

  1. PrincipalContext定义查找第一组的位置,在本例中为“本地计算机”。
  2. 遍历每个组会在查找详细信息时使用组本身的PrincipalContext。如果该组属于Domain,它将使用域上下文自动查找,如果它是本地计算机组,它将使用本地计算机查找。
  3. 回溯地浏览每个组,直到其所有用户帐户都可以查找其成员。