简体   繁体   English

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

[英]How to get a list of local machine groups / users when machine is not in active directory?

当Windows计算机不是AD成员并且无法使用LDAP搜索时,是否可以通过C#获取本地组和用户列表?

You can use P/Invoke to call the native network management API to get local user and group names: 您可以使用P / Invoke调用本地网络管理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);

}

The API allows you to get various information about users. 该API可让您获取有关用户的各种信息。 If you only want names you can use this function: 如果只需要名称,则可以使用此功能:

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);
  }
}

The LINQ statement is used to "parse" the buffer that contains USER_INFO_0 strutures. LINQ语句用于“解析”包含USER_INFO_0结构的缓冲区。 If you are querying for additional information you will have to do more elaborate "parsing". 如果您要查询其他信息,则必须进行更详细的“解析”。

Likewise you can get local group names: 同样,您可以获得本地组名称:

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);
  }
}

The structures in the buffer are LOCALGROUP_INFO_0 with the same layout as the USER_INFO_0 structure so the "parsing" code is identical. 缓冲区中的结构是LOCALGROUP_INFO_0 ,其布局与USER_INFO_0结构相同,因此“解析”代码相同。

Finally, here is how to get group membership using the LOCALGROUP_MEMBERS_INFO_3 structure: 最后,这是如何使用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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地计算机关闭网络确定用户Active Directory组 - Determine User Active Directory Groups from Local Machine off Network 获取嵌套活动目录组中的用户列表 - Get a list of users in nested active directory groups C# - 如何获取有权访问远程计算机上共享文件夹的USER / GROUP列表 - C# - How to get list of USERs/GROUPs having access to shared folder on a Remote Machine 如何获取Active Directory组中的组列表 - How to get a list of groups in an Active Directory group 如何使用托管代码(无P /调用)在本地计算机上获取所有组 - How to get all groups on a local machine with managed code (no P/Invoke) 如何使用c#.net从他在winnt文件夹(本地用户和组)或活动目录(域下)中创建的pc中列出用户 - how to list out users from pc either he created in winnt folder(local users and groups) or active directory( under domain) using c#.net ASP.NET如何获取Active Directory中的组列表 - ASP.NET How to get List of Groups in Active Directory .NET如何在Active Directory中搜索和获取用户列表 - .NET How to search and get list of users in Active Directory 如何从活动目录中获取用户列表? - How can I get a list of users from active directory? 如何使用LINQ to LDAP获取活动目录中的用户列表? - how can get List of users in active directory using LINQ to LDAP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM