【问题标题】:How to get all members of a local WinNT group?如何获取本地 WinNT 组的所有成员?
【发布时间】:2013-03-05 09:20:35
【问题描述】:

当我检索本地 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 组,其余项目也不会显示。因此,如果在此示例中首先列出了“Test_DomainGroup”,则 GetMembers() 根本不会显示任何内容。

【问题讨论】:

    标签: c# .net directoryservices directoryentry


    【解决方案1】:

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

    您在 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 从使用 WinNT 提供程序切换到使用 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多