Home > Enterprise >  Request Administrator Rights at Runtime
Request Administrator Rights at Runtime

Time:01-24

how can i request admin rights at runtime in a winforms C# application? I have seen some installers asking for admin rights after selecting 'install for all users' (at runtime).

I have seen some answers on other questions saying it is impossible to request admin at runtime. I have tried this code:

System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

but it requires current user to be admin to gain the rights.

are there any other methods to request admin at runtime? (please dont suggest creating a manifest file thank you)

CodePudding user response:

You can't add administrator rights to a running process that has already started. It either has admin rights for the life of the process from the moment it starts up, or it doesn't.

To get around this, there are three common strategies here:

  1. Set your app to ALWAYS prompt for administrator rights. You can do this in the properties for the .exe file, without modifying any code in the application itself (You will need to modify your installer).
  2. Split the app's functions to a second (or more) exe file, where the main exe calls into the additional exe(s) for any function requiring elevation.
  3. Rebuild the exe according to best practices, so it no longer needs administrator access. The biggest culprit here is writing into the Program Files folder; if you can instead change the application to save files in the Application Data folder you may find you don't need administrator access at all.

CodePudding user response:

I have discovered how to ask for administrator rights at runtime. Here's how to do it

Add this class to your Program.cs file (alternatively, create standalone static bool)

public static class RequestAdministrator
{
    public static bool request = false;
}

Now you want to check at startup, if your program has been ran with any arguements

static void Main(string[] args)
{
    if (args.Count() == 0 /* or args[0].ToString() == "admin"*/)
    {
        RequestAdministrator.request = false;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    else
    {
        //request admin

        RequestAdministrator.request = true;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

        
}

and then place this code wherever you want your program to request admin rights. It restarts the program with "admin" argument, allowing you to put addictional code in the if statement (e.g. load the same file from previous instance)

//this will try to get the administrator rights if the user is an admin                           
System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        if(IsAdministrator() == false)
        {
            //Init a new instance of the program
            ProcessStartInfo startInfo = new ProcessStartInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, "admin");
            startInfo.UseShellExecute = true;
            startInfo.Verb = "runas";
            System.Diagnostics.Process.Start(startInfo);
            //no need for Application.Exit()
        }

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

at the end, you can use this code to see if the admin right have been requested (you can use IsAdministrator() as well to determine if user declined admin privilleges)

if (RequestAdministrator.request == false)
        {
            //continue without rights (user ran the exe)

            //your code here
            //Step1 step1 = new Step1();
            //this.Controls.Add(step1);
            //step1.Show();
        }
        else
        {
            //request true (restarted with arguments)

            //your code here
            //Step2 step2 = new Step2();
            //this.Controls.Add(step2);
            //step2.Show();
        }

Result

program asking for admin

  • Related