c# - 如何获得本地 WinNT 组的所有成员?

标签 c# .net directoryservices directoryentry

当我检索本地 WinNT 组的成员时,不知为何返回的不是所有 成员。我补充说:

  • 事件目录用户
  • 事件目录组

两者都成功(见图片),但只有用户随后出现。

enter image description here

问题是:

  • 添加的组会怎样?
  • 查看代码示例“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() 根本不会显示任何内容。

最佳答案

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

您在 DirectoryEntry [即。 WinNT://MYDOMAIN/myserver] 处理未停留在旧 Windows 2000/NT 功能级别 (https://support.microsoft.com/en-us/kb/322692) 的 Windows 域的能力非常有限。

在这种情况下,问题是 WinNT 提供程序不知道如何处理全局或通用安全组(它们在 Windows NT 中不存在,并且一旦您将域级别提升到 Windows 2000 混合模式以上就会激活).因此,如果这些类型的任何组嵌套在本地组下,您通常会遇到与您描述的问题类似的问题。

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

不幸的是,我不知道有一种“简单”的方法可以使用您已经绑定(bind)到 WinNT 提供程序的 DirectoryEntry 从使用 WinNT 提供程序切换到使用 LDAP 提供程序。因此,在我从事的项目中,我通常更喜欢获取当前 WinNT 对象的 SID,然后使用 LDAP 搜索具有相同 SID 的域对象。

对于 Windows 2003+ 域,您可以将 SID 字节数组转换为常用的 SDDL 格式(S-1-5-21...),然后使用类似的方法绑定(bind)到具有匹配 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 直接绑定(bind)到对象。因此,您必须将 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();
    }
}

关于c# - 如何获得本地 WinNT 组的所有成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220009/

相关文章:

c# - 从富文本框中获取选中的文本

c# - 如何获取 xml 架构 (xsd) 的 cs 文件

c# - Log4net 日志文件未创建?

.net - 默认值 Specflow 步骤定义

.net - 从 System.DirectoryServices 切换到 DirectoryServices.Protocols 时的身份验证类型

c# - 使用 SSH.NET 在命令输出期间发送输入

c# - Linq - 获取数组最后一个非零数的索引

c# - 使用 TransactionScope 的嵌套事务

c# - 使用 DirectorySearcher.FindAll() 时发生内存泄漏

c# - 在 C# 中是否可以直接读取属性(而不是循环访问 PropertyNames)