How to run some lines of codes with elevated previlege

Answered How to run some lines of codes with elevated previlege

  • Saturday, May 05, 2012 9:32 AM
     
     

    Dear all,

    I have an application which normally works without administrative privileges. There is one activity which does need administrative privilege but I don't want to start the application itself with higher privileges because most of time user won't even be using that feature.

    I am thinking about certain method by which I can elevate the privileges of application on some event (such as press of a button).

    Another way is let the user grant privilege to several lines of codes. Can this be done?

All Replies

  • Saturday, May 05, 2012 9:55 AM
     
     Answered

    Sure. There are basically to approaches:

    1. Use a separate process, a Windows Service, which has the necessary privileges. Use any IPC method to communicate with it.
    2. Use impersonation in your application.

    The first approach makes only sense when you have more than one process which requires elevation. The second could be used to wrap any method in an elevated context.

    See Impersonation. Some examples: Windows Impersonation using C#, A small C# Class for impersonating a User. And some more information here.


  • Saturday, May 05, 2012 10:06 AM
     
     
    Thank you very much!
  • Saturday, May 12, 2012 6:47 AM
     
     

    Excuse me, but what if I don't want the users to provide the user name and password?

    Thanks,

    Nick

  • Saturday, May 12, 2012 11:46 AM
     
     Answered
    Then you should consider the Windows Service approach. The service gets the credentials setup when it is installed once. So the users do not need to provide any information.