Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取本地WinNT组的所有成员?_C#_.net_Directoryservices_Directoryentry - Fatal编程技术网

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

C# 如何获取本地WinNT组的所有成员?,c#,.net,directoryservices,directoryentry,C#,.net,Directoryservices,Directoryentry,当我检索本地WinNT组的成员时,不知何故,并非所有成员都返回。我要补充: Active Directory用户 Active Directory组 两者都成功了(见图),但之后只有用户出现 问题是: 添加的组会发生什么情况 请参阅代码示例“GetMembers()中的最后一个方法” 这是一个已知的问题吗 有解决办法吗 非常感谢 string _domainName = @"MYDOMAIN"; string _basePath = @"WinNT://MYDOMAIN/myserve

当我检索本地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()根本不会显示任何内容。

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

您在DirectoryEntry中使用的WinNT ADSI提供程序[ie.WinNT://MYDOMAIN/myserver]在处理未停留在旧的Windows 2000/NT功能级别()的Windows域时具有相当有限的功能

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

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

不幸的是,我不知道有什么“简单”的方法可以使用您已经绑定到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() + ">");
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();
    }
}
Byte[]SIDBytes=(Byte[])memberEntry.Properties[“objectSID”].Value;
System.Security.Principal.SecurityIdentifier SID=新的System.Security.Principal.SecurityIdentifier(SIDBytes,0);
memberEntry.Dispose();
memberEntry=new DirectoryEntry(“LDAP:/”+_domainName+“/”);
对于Windows 2000域,不能通过SID直接绑定到对象。因此,您必须将SID字节数组转换为带有“\”前缀(\01\06\05\16\EF\A2..)的十六进制值数组,然后使用DirectorySearcher查找具有匹配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() + ">");
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();
    }
}
public DirectoryEntry FindMatchingSID(字节[]SIDBytes,字符串Win2KDNSDomainName)
{
使用(DirectorySearcher search=newdirectorysearcher(“LDAP://”+Win2KDNSDomainName))
{
System.Text.StringBuilder SIDByteString=新的System.Text.StringBuilder(SIDBytes.Length*3);
对于(Int32 sidByteIndex=0;sidByteIndex