.net - User settings/configuration with impersonation -
is possible configurationmanager.openexeconfiguration use impersonated user (when impersonation being done similar code sample windowsimpersonationcontext) - following being small extract?
using (safetokenhandle) { console.writeline("did logonuser succeed? " + (returnvalue ? "yes" : "no")); console.writeline("value of windows nt token: " + safetokenhandle); // check identity. console.writeline("before impersonation: " + windowsidentity.getcurrent().name); configuration config; //config = configurationmanager.openexeconfiguration(configurationuserlevel.peruserroamingandlocal); //console.writeline("local user config path: {0}", config.filepath); // use token handle returned logonuser. using (windowsidentity newid = new windowsidentity(safetokenhandle.dangerousgethandle())) { using (windowsimpersonationcontext impersonateduser = newid.impersonate()) { // check identity. console.writeline("after impersonation: " + windowsidentity.getcurrent().name); // line throws exception config = configurationmanager.openexeconfiguration(configurationuserlevel.peruserroamingandlocal); console.writeline("local user config path: {0}", config.filepath); } } // releasing context object stops impersonation // check identity. console.writeline("after closing context: " + windowsidentity.getcurrent().name); }
if add call inside impersonated scope, exception thrown:
exception occurred. error occurred loading configuration file: catastrophic failure (exception hresult: 0x8000ffff (e_unexpected))
if call openexeconfiguration before impersonation block, second call (inside block) doesn't fail, returns path original user.
there's few things need happen make work:
- the impersonated user's profile needs explicitly loaded using loaduserprofile - not done impersonating user. note api requires calling process must have se_restore_name , se_backup_name privileges.
- if using settings class inherits applicationsettingsbase, need implement custom settingsprovider knows how load config per-user
- the settings properties default cached. need customise getters force reload() each time ensure settingsprovider gets called.
this sample showing how call loaduserprofile api - http://www.codeproject.com/articles/125810/a-complete-impersonation-demo-in-c-net
Comments
Post a Comment