C# 检查当前登录用户是否为管理员(远程机器)

标签 c# windows privileges

我知道有几个关于这个主题的讨论,但没有一个真正回答我的确切问题。 我正在寻找一种方法来远程检查当前登录的用户是否具有管理员权限。 他是本地内置的机器“管理员”组的成员还是“管理员”内嵌套组的成员,例如“域管理员”。 我找到了几种方法,但每种方法都只提供了一半的解决方案。

方法#1(远程工作但只检查本地“管理员”组):

private bool isAdmin()
{
    ArrayList mem2 = new ArrayList();
    string hostName = basicinfomodel.Loggedusername; //a username I get from another class
    try
    {
        using (DirectoryEntry machine = new DirectoryEntry("WinNT://" + mycomputer.myComputerName)) // remote computer that I get from another class
        {
            //get local admin group
            using (DirectoryEntry group = machine.Children.Find("Administrators", "Group"))
            {
                //get all members of local admin group
                object members = group.Invoke("Members", null);
                foreach (object member in (IEnumerable)members)
                {
                    //get account name
                    string accountName = new DirectoryEntry(member).Name;
                    mem2.Add(new DirectoryEntry(member).Name);
                }
            }
        }
    }
    catch (Exception ex)
    {
        // catch
    }

    if (mem2.Contains(hostName.ToUpper()) || mem2.Contains(hostName.ToLower()))
        return true;
    else
        return false;
}

方法#2(检查本地和域管理员权限,但不远程工作)

static bool isAdmin()
{
    WindowsIdentity User = new WindowsIdentity(@"user01");
    WindowsPrincipal princ = new WindowsPrincipal(User);
    return princ.IsInRole(WindowsBuiltInRole.Administrator);
}

正如我所说,我没有找到任何方法来满足这两种需求。

  1. 检查用户是否真的拥有管理员权限
  2. 远程操作

感谢您的帮助!

最佳答案

好吧,我想我找到了一种方法,我正在分享以防其他人想要使用它。 我使用了我发现的几种方法并创建了以下方法(似乎有效)

static bool isAdmin(string username, string machinename)
{
    using (PrincipalContext ctxMacine = new PrincipalContext(ContextType.Machine, machinename))
    {
        using (PrincipalContext ctxDomain = new PrincipalContext(ContextType.Domain))
        {
            UserPrincipal up = UserPrincipal.FindByIdentity(ctxDomain, IdentityType.SamAccountName, username);
            GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctxMacine, "Administrators");

            foreach (UserPrincipal usr in gp.GetMembers(true))
            {
                if (up != null)
                {
                    if (up.SamAccountName.ToUpper() == usr.SamAccountName.ToUpper())
                    {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

注意
这是一个天真的实现,您应该验证您的代码,检查 null 并处理异常。

关于C# 检查当前登录用户是否为管理员(远程机器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26298556/

相关文章:

c# - BackgroundWorker - 调用线程无法访问对象

C# - 为什么直方图在 Excel 2016 中不起作用?

c# - 如何在 Quartz.net 中随时开始工作?

c++ - 处理高速数据时避免 QSerialPort 超时

c++ - 如何只运行一个应用程序实例

inno-setup - "The requested operation requires elevation"设置后的消息

windows - 在 Windows 上将用户模拟为 LocalSystem

c# - 使用 WinForms ProgressBar 异步/等待

c# - 获取登录用户的 AppData\Local 文件夹

c++ - 我想从 Python 调用 Windows C++ 函数 WinHttpGetProxyForUrl - 这可以做到吗?