Check if the current user is administrator

Fliskov picture Fliskov · Aug 30, 2010 · Viewed 50.2k times · Source

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#?

Answer

Nissim picture Nissim · Aug 30, 2010
using System.Security.Principal;

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