【问题标题】:Get a list of members of a WinNT group获取 WinNT 组的成员列表
【发布时间】:2008-10-31 08:33:04
【问题描述】:

堆栈溢出有几个类似的问题,但并不完全相同。

我想在 win xp 计算机上打开或创建一个本地组并向其添加成员、域、本地和知名帐户。我还想检查一个用户是否已经是会员,这样我就不会两次添加同一个帐户,并且可能会出现异常。

到目前为止,我开始将 DirectoryEntry 对象与 WinNT:// 提供程序一起使用。这没问题,但我不知道如何获取组成员的列表?

有人知道怎么做吗?或者提供比使用 DirectoryEntry 更好的解决方案?

【问题讨论】:

    标签: c# .net active-directory active-directory-group windows-security


    【解决方案1】:

    好的,花了一段时间,弄乱了不同的解决方案,但下面给出了最适合我原始问题的解决方案。我无法让 DirectoryEntry 对象使用“标准”方法访问本地组的成员,我可以让它枚举成员的唯一方法是使用 Invoke 方法调用本机对象的成员方法。

    using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group")) { foreach((IEnumerable) groupEntry.Invoke("Members") 中的对象成员) { 使用(目录条目成员条目 = 新目录条目(成员)) { Console.WriteLine(memberEntry.Path); } } }

    我还使用类似的技术在本地组中添加和删除成员。

    希望这对其他人也有帮助。 基思。

    EDIT by Tim:添加了 VB.Net 版本

    Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
        Dim members As New List(Of DirectoryEntry)
        Try
            Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
                For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
                    Dim memberEntry As New DirectoryEntry(member)
                    members.Add(memberEntry)
                Next
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return members
    End Function
    

    【讨论】:

    • 它不工作显示此错误:错误 4 使用泛型类型 'System.Collections.Generic.IEnumerable' 需要 '1' 类型参数 C:\Documents and Settings\pratikj\My Documents \Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 112 44 ConsoleApplication1 你能帮帮我吗??
    • 我知道有点晚了,但你需要使用 System.Collections.IEnumerable
    【解决方案2】:

    Microsoft .NET Framework 提供了一个用于使用 Active Directory 的标准库:System.DirectoryServices namespace 在 System.DirectoryServices.dll 中。

    Microsoft 建议使用 System.DirectoryServices 命名空间中的两个主要类:DirectoryEntryDirectorySearcher。大多数情况下,只使用 DirectorySearcher 类就足够了。

    更新:我在我的机器上测试了它——它可以工作。但也许我误解了 你的问题。

    这里有一个来自优秀CodeProject article的例子:

    获取属于特定 AD 组的用户列表

    using System.DirectoryServices;
    
    ArrayList GetADGroupUsers(string groupName)
    {    
       SearchResult result;
       DirectorySearcher search = new DirectorySearcher();
       search.Filter = String.Format("(cn={0})", groupName);
       search.PropertiesToLoad.Add("member");
       result = search.FindOne();
    
       ArrayList userNames = new ArrayList();
       if (result != null)
       {
           for (int counter = 0; counter < 
              result.Properties["member"].Count; counter++)
           {
               string user = (string)result.Properties["member"][counter];
                   userNames.Add(user);
           }
       }
       return userNames;
    }
    

    【讨论】:

    • 我认为您的代码仅适用于活动目录组。我需要得到一个“本地”组的成员。就像在所有 Windows 安装上设置的本地管理员组一样,可能不是 Vista。
    • 我尝试了这两种方法来获取本地组的成员。接受的答案对我有用,而你给出的答案没有。我确信它对 AD 组工作得很好,但我收到一条错误消息,说 WinNT:// 对象不支持搜索。
    【解决方案3】:

    您应该能够在代表该组的DirectoryEntry 上的"member" attribute 中找到此信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 2014-06-23
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多