Given your username and password, how do you impersonate this user?

On Windows Vista and later, LogonUser returns a non-privileged token, even if the user is provided by an administrator. That way, when you personalize the use of this token, you are not increasing. Given the correct username and password for the administrator user, how do you run code with elevated privileges for this administrator?

+6
source share
2 answers

If you are writing a Windows service, using LOGON32_LOGON_SERVICE instead of LOGON32_LOGON_INTERACTIVE or LOGON32_LOGON_NEW_CREDENTIALS will result in a privileged token if the user you want to impersonate has the "log in as a service" access rights.

This does not provide a general solution (it will not work if you are writing a user-oriented application), but this is enough to solve a specific problem.

0
source

I just worked with this example , it actually works fine. I assume that if you want to run the code in this context, you will need to set the current thread directive to:

 Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(WindowsIdentity.GetCurrent()); 
+1
source

Source: https://habr.com/ru/post/954775/


All Articles