impersonation - Impersonate admin account to edit registry key not working (C#) -


i using following code edit registry key in local machine hive ('software\microsoft\windows nt\currentversion\profilelist\%sid%'). seems fine until try open registry key (with write permissions); securityexception thrown message 'requested registry access not allowed.' i've checked , rechecked permissions registry key , user i'm impersonating , checks out. code runs fine when logged impersonated user's account, when logged in restricted user, fails. it's if impersonation works except giving thread administrative privileges. ideas how fix appreciated!

string key_str = "software\\microsoft\\windows nt\\currentversion\\profilelist\\" + windowsidentity.getcurrent().user.value; windowsimpersonationcontext admincontext = null; intptr tokenhandle = new intptr(0); try {     logonuser(username, domainname, password, logon32_logon_interactive, logon32_provider_default, ref tokenhandle);     if (tokenhandle.equals(new intptr(0))) logonuser(username, computername, password, logon32_logon_interactive, logon32_provider_default, ref tokenhandle);     windowsidentity adminidentity = new windowsidentity(tokenhandle);     admincontext = adminidentity.impersonate();     registrykey key = registry.localmachine.opensubkey(key_str, true);     key.setvalue("state", 0x60001);     console.out.writeline("user profile changed mandatory."); } catch (exception ex) {     console.out.writeline("\nunable set profile mandatory:\n\t" + ex.message); } {     admincontext.undo();     if (tokenhandle != intptr.zero) closehandle(tokenhandle); } 

just taking guess here, happen on vista or win7 box uac turned on? i'm willing have have user confirmation elevate permissions this.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -