Run as administrator: requireAdministrator & ClickOnce + emulating system time

后端 未结 6 1712
暗喜
暗喜 2020-12-18 21:33

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



        
相关标签:
6条回答
  • 2020-12-18 22:09

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

    0 讨论(0)
  • 2020-12-18 22:10

    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.

    0 讨论(0)
  • 2020-12-18 22:10

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

    0 讨论(0)
  • 2020-12-18 22:11
       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 :-)");
            }
        }
    
    0 讨论(0)
  • 2020-12-18 22:27

    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.

    0 讨论(0)
  • 2020-12-18 22:28

    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.

    0 讨论(0)
提交回复
热议问题