如何获取本地WinNT组的所有成员?

3
当我检索本地WinNT组的成员时,不知何故并没有返回所有成员。我添加了以下内容:
  • Active Directory用户
  • Active Directory组
两者都成功(见图片),但之后只有用户出现。
问题是:
  • 添加的组发生了什么?
  • 请参阅代码示例中的最后一个方法“GetMembers()”
  • 这是已知问题吗?
  • 是否有任何可用的解决方法?
非常感谢!
string _domainName = @"MYDOMAIN";
string _basePath = @"WinNT://MYDOMAIN/myserver";
string _userName = @"MYDOMAIN\SvcAccount";
string _password = @"********";

void Main()
{
   CreateGroup("lg_TestGroup");
   AddMember("lg_TestGroup", @"m.y.username");
   AddMember("lg_TestGroup", @"Test_DomainGroup");

   GetMembers("lg_TestGroup");
}

// Method added for reference.
void CreateGroup(string accountName)
{
   using (DirectoryEntry rootEntry = new DirectoryEntry(_basePath, _userName, _password))
   {
      DirectoryEntry newEntry = rootEntry.Children.Add(accountName, "group");
        newEntry.CommitChanges();
   }
}

// Add Active Directory member to the local group.
void AddMember(string groupAccountName, string userName)
{
    string path = string.Format(@"{0}/{1}", _basePath, groupAccountName);
    using (DirectoryEntry entry = new DirectoryEntry(path, _userName, _password))
    {
        userName = string.Format("WinNT://{0}/{1}", _domainName, userName);
      entry.Invoke("Add", new object[] { userName });
        entry.CommitChanges();
    }
}

// Get all members of the local group.
void GetMembers(string groupAccountName)
{
    string path = string.Format(@"{0}/{1}", _basePath, groupAccountName);
   using (DirectoryEntry entry = new DirectoryEntry(path, _userName, _password))
   {
      foreach (object member in (IEnumerable) entry.Invoke("Members"))
      {
         using (DirectoryEntry memberEntry = new DirectoryEntry(member))
         {
            string accountName = memberEntry.Path.Replace(string.Format("WinNT://{0}/", _domainName), string.Format(@"{0}\", _domainName));
            Console.WriteLine("- " + accountName); // No groups displayed...
         }
      }
   }
}

更新 #1 群组成员的顺序似乎是至关重要的。一旦在 GetMembers() 中的枚举器遇到一个Active Directory group,剩余的项也不会被显示。因此,如果在这个例子中首先列出'Test_DomainGroup',GetMembers() 将不会显示任何内容。

1个回答

3

我知道这是一个老问题,你可能已经找到了需要的答案,但以防万一其他人也遇到了这个问题...

你在DirectoryEntry中使用的WinNT ADSI提供程序[即WinNT://MYDOMAIN/myserver]在处理不是被困在旧的Windows 2000/NT功能级别(https://support.microsoft.com/en-us/kb/322692)中的Windows域时,其功能相当有限。

在这种情况下,问题在于WinNT提供程序不知道如何处理全局或通用安全组(它们在Windows NT中不存在,并且在将域级别提高到Windows 2000混合模式以上后立即激活)。因此,如果这些类型的任何组嵌套在本地组下面,你通常会遇到像你描述的那样的问题。

我找到的唯一解决方案/解决方法是确定你正在枚举的组是否来自域,如果是,则切换到LDAP提供程序,在调用“成员”时将正确显示所有成员。

很遗憾,我不知道有什么“简单”的方法可以直接从已绑定到WinNT提供程序的DirectoryEntry切换到使用LDAP提供程序。因此,在我参与的项目中,我通常更喜欢获取当前WinNT对象的SID,然后使用LDAP搜索具有相同SID的域对象。
对于Windows 2003+域,您可以将SID字节数组转换为通常的SDDL格式(S-1-5-21...),然后使用类似以下内容的内容绑定到具有匹配SID的对象:
Byte[] SIDBytes = (Byte[])memberEntry.Properties["objectSID"].Value;
System.Security.Principal.SecurityIdentifier SID = new System.Security.Principal.SecurityIdentifier(SIDBytes, 0);

memberEntry.Dispose();
memberEntry = new DirectoryEntry("LDAP://" + _domainName + "/<SID=" + SID.ToString() + ">");

对于Windows 2000域,您无法直接通过SID绑定到对象。因此,您需要将SID字节数组转换为带有“\”前缀(\01\06\05\16\EF\A2..)的十六进制值数组,然后使用DirectorySearcher查找具有匹配SID的对象。一个执行此操作的方法可能如下:

public DirectoryEntry FindMatchingSID(Byte[] SIDBytes, String Win2KDNSDomainName)
{
    using (DirectorySearcher Searcher = new DirectorySearcher("LDAP://" + Win2KDNSDomainName))
    {
        System.Text.StringBuilder SIDByteString = new System.Text.StringBuilder(SIDBytes.Length * 3);

        for (Int32 sidByteIndex = 0; sidByteIndex < SIDBytes.Length; sidByteIndex++)
            SIDByteString.AppendFormat("\\{0:x2}", SIDBytes[sidByteIndex]);

        Searcher.Filter = "(objectSid=" + SIDByteString.ToString() + ")";
        SearchResult result = Searcher.FindOne();

        if (result == null)
            throw new Exception("Unable to find an object using \"" + Searcher.Filter + "\".");
        else
            return result.GetDirectoryEntry();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接