Here is the scenario:
1. The app is already running as a local admin
2. It impersonates as a domain account, which is also an admin on the local box
3. While impersonated, the app is trying to create a regkey under the key that has Full Control to the Administrators group for “This key and subkeys.”
This step fails with UnauthorizedException “Access to registry key ..” is denied.
Now, if I explicitly ACL the regkey for the domain user, the creation of the regkey goes through. But then this solution defeats the purpose of being in the admin group.
Any ideas what could go wrong here?
EDIT: I’m running on Windows Server 2008 R2. I figured this issue is due to UAC enabled. LogonUser method returns a restricted token, which does not have elevated access to the registry. Any ideas on how to get an elevated access using LogonUser method?
Here is how I call it:
IntPtr token = IntPtr.Zero;
LogonUser(username, domain, password, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, out token)
I will suggest several things to check:
You should attribute your class (that executes impersonation) for a full trust mode request, which you can do using
Also, you should import “advapi32.dll” and LogonUser to be used later.
Within this you should be calling it like this (*using LOGON32_LOGON_INTERACTIVE since BATCH will not work*)
After getting a handle, you should use it to perform any action:
After getting it, encapsulate your actions:
This should enable you to do it correctly and detect how it went.
I have now tried to do this in an ASP.NET application and succeded. Here is a working code for an MVC application controller: