In .NET/C# test if process has administrative privileges

前端 未结 10 1944
天涯浪人
天涯浪人 2020-11-27 03:35

Is there a canonical way to test to see if the process has administrative privileges on a machine?

I\'m going to be starting a long running process, and much later

相关标签:
10条回答
  • 2020-11-27 04:27

    Use can use WMI with something like this to find out if the account is an admin, and just about anything else you want to know about there account

    using System;
    using System.Management;
    using System.Windows.Forms;
    
    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher("root\\CIMV2", 
                        "SELECT * FROM Win32_UserAccount"); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_UserAccount instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("AccountType: {0}", queryObj["AccountType"]);
                        Console.WriteLine("FullName: {0}", queryObj["FullName"]);
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }
    

    To make it easier to get started download WMI Creator

    you can also use this it access active directory (LDAP) or anything else on you computer/network

    0 讨论(0)
  • 2020-11-27 04:30

    This will check if user is in the local Administrators group (assuming you're not checking for domain admin permissions)

    using System.Security.Principal;
    
    public bool IsUserAdministrator()
    {
        //bool value to hold our return value
        bool isAdmin;
        WindowsIdentity user = null;
        try
        {
            //get the currently logged in user
            user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch (UnauthorizedAccessException ex)
        {
            isAdmin = false;
        }
        catch (Exception ex)
        {
            isAdmin = false;
        }
        finally
        {
            if (user != null)
                user.Dispose();
        }
        return isAdmin;
    }
    
    0 讨论(0)
  • 2020-11-27 04:31

    If you want to make sure your solution works in Vista UAC, and have .Net Framework 3.5 or better, you might want to use the System.DirectoryServices.AccountManagement namespace. Your code would look something like:

    bool isAllowed = false;
    using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
    {
        UserPrincipal up = UserPrincipal.Current;
        GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators");
        if (up.IsMemberOf(gp))
            isAllowed = true;
    }
    
    0 讨论(0)
  • 2020-11-27 04:32

    What about:

    using System.Runtime.InteropServices;
    
    internal static class Useful {
        [DllImport("shell32.dll", EntryPoint = "IsUserAnAdmin")]
        public static extern bool IsUserAnAdministrator();
    }
    
    0 讨论(0)
提交回复
热议问题