Search code examples
c#windows-administration

Check if the current user is administrator


My application needs to run some scripts, and I must be sure that the user running them is an administrator... What is the best way of doing this using C#?


Solution

  • using System.Security.Principal;
    
    public static bool IsAdministrator()
    {
        using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
        {
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }