如何检查系统上的每个用户是否在 C# 中具有管理员权限
How to check if every users on the system has administrator rights in C#
我有一个在我的系统中创建的用户列表:
- 管理员(默认)
- 客人
- 用户 1(标准用户)
- User2(管理员用户)
我想知道在 C# 中通过 WMI 赋予所有这些用户的权限
,这怎么可能?有没有其他方法可以找到它们。
即使一个用户有这个权利,它也必须退出循环
我使用下面的代码:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (isAdmin == true)
{
current_logged_user = "Yes";
}
else
{
current_logged_user = "No";
}
这只给我当前记录的信息,但我需要所有用户
link
下面link只给管理员成员
link
你可以试试这个:
bool IsInGroup(string user, string group)
{
using (var identity = new WindowsIdentity(user))
{
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(group);
}
}
您可以将 IsInRole(group) 更改为 IsInRole(WindowsBuiltInRole.Administrator)
你有域服务器吗?
您应该能够 return 所有用户通过 WMI
string groupNameToSearchFor = "Administrators"; // can be any group,maybe better to use something like builtin.administrators
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
{
ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
ManagementObjectCollection users = usersSearcher.Get();
foreach (ManagementObject user in users)
{
if ((bool)user["LocalAccount"] == true && int.Parse(user["SIDType"].ToString()) == 1)
{
var userPrincipal = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, user["Name"].ToString());
GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, groupNameToSearchFor);
MessageBox.Show("Is User admin? -> " + (bool)userPrincipal.IsMemberOf(gp));
}
}
}
您必须包含
的用法
using System.DirectoryServices.AccountManagement;
using System.Management;
还要检查用户是否真的是用户而不是其他对象(不确定我的检查是否足够)。
编辑:在
获得列表后,您可以投射您需要的用户
var localUsers = users.Cast<ManagementObject>().Where(
u => (bool)u["LocalAccount"] == true &&
(bool)u["Disabled"] == false &&
(bool)u["Lockout"] == false &&
int.Parse(u["SIDType"].ToString()) == 1 &&
u["Name"].ToString() != "HomeGroupUser$");
我有一个在我的系统中创建的用户列表:
- 管理员(默认)
- 客人
- 用户 1(标准用户)
- User2(管理员用户)
我想知道在 C# 中通过 WMI 赋予所有这些用户的权限 ,这怎么可能?有没有其他方法可以找到它们。 即使一个用户有这个权利,它也必须退出循环
我使用下面的代码:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (isAdmin == true)
{
current_logged_user = "Yes";
}
else
{
current_logged_user = "No";
}
这只给我当前记录的信息,但我需要所有用户
link
下面link只给管理员成员 link
你可以试试这个:
bool IsInGroup(string user, string group)
{
using (var identity = new WindowsIdentity(user))
{
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(group);
}
}
您可以将 IsInRole(group) 更改为 IsInRole(WindowsBuiltInRole.Administrator)
你有域服务器吗?
您应该能够 return 所有用户通过 WMI
string groupNameToSearchFor = "Administrators"; // can be any group,maybe better to use something like builtin.administrators
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
{
ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
ManagementObjectCollection users = usersSearcher.Get();
foreach (ManagementObject user in users)
{
if ((bool)user["LocalAccount"] == true && int.Parse(user["SIDType"].ToString()) == 1)
{
var userPrincipal = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, user["Name"].ToString());
GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, groupNameToSearchFor);
MessageBox.Show("Is User admin? -> " + (bool)userPrincipal.IsMemberOf(gp));
}
}
}
您必须包含
的用法using System.DirectoryServices.AccountManagement;
using System.Management;
还要检查用户是否真的是用户而不是其他对象(不确定我的检查是否足够)。
编辑:在
获得列表后,您可以投射您需要的用户 var localUsers = users.Cast<ManagementObject>().Where(
u => (bool)u["LocalAccount"] == true &&
(bool)u["Disabled"] == false &&
(bool)u["Lockout"] == false &&
int.Parse(u["SIDType"].ToString()) == 1 &&
u["Name"].ToString() != "HomeGroupUser$");