C#检查当前登录的用户是否为Admin(远程计算机)

本文关键字:Admin 计算机 是否 检查 登录 用户 | 更新日期: 2023-09-27 18:26:07

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

方法#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. 远程执行

谢谢你的帮助!

C#检查当前登录的用户是否为Admin(远程计算机)

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

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并处理异常。

上面的解决方案很好,但对于不同的情况,它可能会抛出太多异常,并且您必须是该远程计算机上的管理员才能毫无例外地运行此方法。

所以。。我不想捕捉所有可能的异常,我想尽快做到这一点。

所以对我来说,这是对远程机器上当前用户管理权限最好、最快的检查:

    public static bool AdminCheck(string machineName)
    {
        if (Directory.Exists(string.Format("''''{0}''admin$", machineName)))
        {
            return true;
        }
        return false;
    }

没有管理权限,您无法访问管理共享,而且当有人删除它们时,它们会自动重新创建。老实说。。我不认为有人会试图删除操作系统文件夹。

您可以在这里找到更多详细信息:https://en.wikipedia.org/wiki/Administrative_share