当计算机不在活动目录中时,如何获取本地计算机组/用户的列表?

时间:2015-11-25 18:29:37

标签: c# windows account

当Windows机器没有AD成员且无法使用LDAP搜索时,有没有办法让C#进入本地组和用户列表?

1 个答案:

答案 0 :(得分:1)

您可以使用P / Invoke调用native network management API来获取本地用户名和组名:

static class NativeMethods {

  [DllImport("netapi32.dll")]
  public static extern void NetApiBufferFree(IntPtr bufptr);

  [DllImport("netapi32.dll")]
  public static extern UInt32 NetUserEnum([MarshalAs(UnmanagedType.LPWStr)] String servername, UInt32 level, UInt32 filter, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);

  [DllImport("netapi32.dll")]
  public static extern UInt32 NetLocalGroupEnum([MarshalAs(UnmanagedType.LPWStr)] String servername, UInt32 level, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);

  [DllImport("Netapi32.dll")]
  public extern static UInt32 NetLocalGroupGetMembers([MarshalAs(UnmanagedType.LPWStr)] String servername, [MarshalAs(UnmanagedType.LPWStr)] String localgroupname, UInt32 level, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);

}

API允许您获取有关用户的各种信息。如果您只想要名字,可以使用此功能:

IEnumerable<String> GetUserNames() {
  var buffer = IntPtr.Zero;
  try {
    UInt32 entriesRead = 0;
    UInt32 totalEntries = 0;
    var result = NativeMethods.NetUserEnum(null, 0, 0, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
    if (result != 0)
      throw new Win32Exception((Int32) result);
    var userNames = Enumerable
      .Range(0, (Int32) entriesRead)
      .Select(
        i => {
          var userInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
          var userName = Marshal.PtrToStringAuto(userInfo);
          return userName;
        }
      )
      .ToList();
    return userNames;
  }
  finally {
    NativeMethods.NetApiBufferFree(buffer);
  }
}

LINQ语句用于“解析”包含USER_INFO_0个结构的缓冲区。如果您要查询其他信息,则必须进行更详细的“解析”。

同样,您可以获取本地组名称:

IEnumerable<String> GetLocalGroupNames() {
  var buffer = IntPtr.Zero;
  try {
    UInt32 entriesRead = 0;
    UInt32 totalEntries = 0;
    var result = NativeMethods.NetLocalGroupEnum(null, 0, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
    if (result != 0)
      throw new Win32Exception((Int32) result);
    var localGroupNames = Enumerable
      .Range(0, (Int32) entriesRead)
      .Select(
        i => {
          var localGroupInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
          var groupName = Marshal.PtrToStringAuto(localGroupInfo);
          return groupName;
        }
      )
      .ToList();
    return localGroupNames;
  }
  finally {
    NativeMethods.NetApiBufferFree(buffer);
  }
}

缓冲区中的结构为LOCALGROUP_INFO_0,其布局与USER_INFO_0结构相同,因此“解析”代码完全相同。

最后,以下是如何使用LOCALGROUP_MEMBERS_INFO_3结构获取组成员身份:

IEnumerable<String> GetLocalGroupUsers(String localGroupName) {
  var buffer = IntPtr.Zero;
  try {
    UInt32 entriesRead = 0;
    UInt32 totalEntries = 0;
    var result = NativeMethods.NetLocalGroupGetMembers(null, localGroupName, 3, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
    if (result != 0)
      throw new Win32Exception((Int32) result);
    var userNames = Enumerable
      .Range(0, (Int32) entriesRead)
      .Select(
        i => {
          var membersInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
          var userName = Marshal.PtrToStringAuto(membersInfo );
          return userName;
        }
      )
      .ToList();
    return userNames;
  }
  finally {
    NativeMethods.NetApiBufferFree(buffer);
  }
}