Run as administrator: requireAdministrator & ClickOnce + emulating system time
Asked Answered
M

6

15

My app uses ClickOnce tehcnology. Today I needed to run it as administrator. I modified the manifest file from

<requestedExecutionLevel  level="asInvoker" uiAccess="false" />

to

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

However VS cannot compile the project:

Error 35 ClickOnce does not support the request execution level 'requireAdministrator'.

I think it's impossible to use them at once. Isn't it? I need to change the system time, can I do that in application level? Can I emulate it, so app. can do what I want. I change time +2 hours then put back for a second. I got a few dlls and they ask for time.

Mientao answered 19/4, 2011 at 8:49 Comment(0)
M
8

Time is a system-wide thing, you can't change it just for your process. The only way to lie about it to your dependencies is to hook the API, using Detours or something similar. Not allowed if you're a lowly user account.

Modifying the time requires the "Change the system time" and/or "Change the time zone" privileges (which the Administrator account is normally given).

And as mentioned by @Chris, admin and ClickOnce aren't compatible.

Momentary answered 27/4, 2011 at 4:13 Comment(2)
Then how would you install an application that needs to run as administrator?Giga
I have had success running a ClickOnce app tha required admin privileges by first logging in as a domain admin user, then running the clickOnce appBarefoot
T
31

Actually You can't run ClickOnce application with Administrative privileges but there is a little hack, you can start new process with Administrator privileges. In App_Startup:

if (!IsRunAsAdministrator())
{
  var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);

  // The following properties run the new process as administrator
  processInfo.UseShellExecute = true;
  processInfo.Verb = "runas";

  // Start the new process
  try
  {
    Process.Start(processInfo);
  }
  catch (Exception)
  {
    // The user did not allow the application to run as administrator
    MessageBox.Show("Sorry, this application must be run as Administrator.");
  }

  // Shut down the current process
  Application.Current.Shutdown();
}

private bool IsRunAsAdministrator()
{
  var wi = WindowsIdentity.GetCurrent();
  var wp = new WindowsPrincipal(wi);

  return wp.IsInRole(WindowsBuiltInRole.Administrator);
}

Read full article.

But if you want more native and easier solution just ask a user to run Internet Explorer as administrator, ClickOnce tool also will run with admin rights.

Tabes answered 4/10, 2013 at 12:18 Comment(5)
It's a good workaround and works! But you can't read a manifest file anymore, because you're not using the .application, you'll use the .exe itself.Babysitter
Man, you made my day with this. Although at first I made the mistake of not checking if my app was running as administrator so it just kept opening the same executable over and over again in a loop.Farron
I tried it and it did not prompt for any UAC box. Then it ran into exception. Does this solution also work for console apps ?Adumbrate
Works fine but sometimes I do not see application icon below status bar.Tackle
This helped, but then my user.config file location was different when I restarted the app. I ended up using a SettingsProvider like the one in #2265771 to set the location to C:\Users\<username>\AppData\Local\<appname>\user.configAustinaustina
M
8

Time is a system-wide thing, you can't change it just for your process. The only way to lie about it to your dependencies is to hook the API, using Detours or something similar. Not allowed if you're a lowly user account.

Modifying the time requires the "Change the system time" and/or "Change the time zone" privileges (which the Administrator account is normally given).

And as mentioned by @Chris, admin and ClickOnce aren't compatible.

Momentary answered 27/4, 2011 at 4:13 Comment(2)
Then how would you install an application that needs to run as administrator?Giga
I have had success running a ClickOnce app tha required admin privileges by first logging in as a domain admin user, then running the clickOnce appBarefoot
B
6

Correct - ClickOnce cannot operator with Administrator priviledges. In fact, it is designed not to.

Billion answered 27/4, 2011 at 3:18 Comment(0)
N
6
   private void Form1_Load(object sender, EventArgs e)
    {
        if (WindowsIdentity.GetCurrent().Owner == WindowsIdentity.GetCurrent().User)   // Check for Admin privileges   
        {
            try
            {
                this.Visible = false;
                ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath); // my own .exe
                info.UseShellExecute = true;
                info.Verb = "runas";   // invoke UAC prompt
                Process.Start(info);
            }
            catch (Win32Exception ex)
            {
                if (ex.NativeErrorCode == 1223) //The operation was canceled by the user.
                {
                    MessageBox.Show("Why did you not selected Yes?");
                    Application.Exit();
                }
                else
                    throw new Exception("Something went wrong :-(");
            }
            Application.Exit();
        }
        else
        {
            //    MessageBox.Show("I have admin privileges :-)");
        }
    }
Norway answered 13/10, 2016 at 9:37 Comment(0)
T
3

If you launching ClickOnce app from IE, to have Administrative privileges just run IE with Administrative privileges and your app will have it too.

Tabes answered 6/12, 2013 at 12:31 Comment(0)
C
1

My app accesses the registry and as such, requires administrator permissions so my project properties / security tab is set for full rights. I deploy my clickonce app to a network drive. I can only run the ClickOnce "setup.exe" as administrator to install my app. However, I'm unable to run my app after it's installed. I cannot run my app as administrator, but I can go back to the setup.exe and run as administrator again which will run my app since it is already installed. Going through the Win 10 Compatibility wizard to set my app to always run with elevated permissions doesn't work. I don't like telling my users they have to always right click and run the setup.exe as administrator from the network drive. In the end, I don't "deploy" the clickonce app. I just build to a network drive and my users run my app's exe. They're then able to use the compatibility wizard to execute as admin all the time.

Crean answered 30/1, 2020 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.