Starting another process with elevation using different user credentials

前端 未结 5 939
旧巷少年郎
旧巷少年郎 2021-02-04 02:00

I\'m trying to start an elevated process from with a non-elevated process, but I also need to supply the username and password for a user with administrative credentials. I\'ve

5条回答
  •  面向向阳花
    2021-02-04 02:33

    From MSDN:

    You cannot elevate an already running process. Thus, you should refactor your app to be separated into admin & non-admin operations - running the default application with normal privileges and starting another elevated process for each administrative operation.

    Let's work with that, assuming you request administrator rights from the outset on the processes that require them. Based upon the context you've provided:

    The issue seems to be setting UseShellExecute to false (as both approaches work fine when this is not the case), but I have to set it to false in order to launch the process under a different user account.

    As you mentioned, exactly as noted in the documentation for UseShellExecute:

    UseShellExecute must be false if the UserName property is not Nothing or an empty string, or an InvalidOperationException will be thrown when the Process.Start(ProcessStartInfo) method is called.

    We now know you're executing your program directly instead of through the use of a shell. This is valuable information.

    Backpathing through the documentation, the docs for ProcessStartInfo carry the following security note:

    This class contains a link demand at the class level that applies to all members. A SecurityException is thrown when the immediate caller does not have full-trust permission. For details about security demands, see Link Demands.

    So, you don't have the right Link Demand. While trying to solve your permissions issue, you inadvertently created another permissions issue.

    The upshot is you need to decorate your calling method with the right Security Demand, which should be FullTrust. You can do this declaratively or imperatively within your code.

    (Additional reading)

提交回复
热议问题