2

I have a method and want to run it with new thread with admin privilege,

[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
    void Install()
    {
      //do some thing
    }

I run it in this way,

installerTh = new Thread(new ThreadStart(Install));
        installerTh.Name = "Installer";
        installerTh.Start();

but it gives me error

System.Security.SecurityException was unhandled

Message=Request for principal permission failed.

Any Idea to show UAC window before running this thread ? (or in the middle of running Process )

Community
  • 1
  • 1
AliRezza
  • 1,267
  • 3
  • 15
  • 26
  • have you seen this question: http://stackoverflow.com/questions/17533/request-windows-vista-uac-elevation-if-path-is-protected – Daniel A. White Oct 29 '11 at 12:55
  • 1
    Hi Daniel , they explain how to start new privileged process,I want to give privilege to my current running Thread or Process. – AliRezza Oct 29 '11 at 13:00

1 Answers1

4

UAC is all or nothing. You cannot apply a UAC prompt to a single thread, only to a process. What you need to do is launch your application with a special command line using UAC, and the command line will let you know to start the thread you want. It does not have to be a command line, you could use any type of IPC to let the spawned process know to run the thread. I wrote an answer about launching a process under UAC here.

You might also want to consider getting a code signing certificate so that the UAC dialog won't keep saying "Unknown" as the publisher.

Update: Based on your comment above, you also cannot apply UAC to your process currently running, only to a new process.

Community
  • 1
  • 1
  • 1
    Thanks dear Matthew,I are you sure only new process ? there is no way for running one, because VS.NET needs to restart when wants to do sth that needs privileged access. – AliRezza Oct 29 '11 at 13:12