c# - Executing powershell threads in IIS as impersonated user -


i've been working on web application makes use of powershell scripts. reason, asp.net impersonation, getting "access denied" errors on commands require elevated access.

the web application deployed via iis 7.5 running on windows 2008 r2 standard sp1 server.

i've checked exception logged in event viewer , noticed account being used in spawned thread network service account :

network service exception

from can tell, means impersonation not being carried powershell cmdlet threads. confident user impersonated should have access run script looks permissions being used of network service account not goal.

i've made following changes aspnet.config file suggested in few articles i've read no avail:

       <legacyimpersonationpolicy enabled="false"/>        <alwaysflowimpersonationpolicy enabled="true"/> 

here's snippet of asp net c# code explain situation:

            //create runspace             runspaceconfiguration psconfig = runspaceconfiguration.create();             runspace psrunspace = runspacefactory.createrunspace(psconfig);              //configure runspace run on current thread             psrunspace.apartmentstate = system.threading.apartmentstate.sta;             psrunspace.threadoptions = system.management.automation.runspaces.psthreadoptions.usecurrentthread;              using (pipeline pspipeline = psrunspace.createpipeline())             {                 psrunspace.open();                 pspipeline.commands.addscript("c:\\scripts\\powershell\\myscript.ps1");                  // invoke cmdlet                  var results = pspipeline.invoke();                 var builder = new stringbuilder();                  foreach (var psobject in results)                 {                     // convert base object string , append string builder.                     // add \r\n line breaks                     builder.append(psobject.baseobject.tostring() + "\r\n");                 }                 //display results                 resultbox.text = builder.tostring(); 

i have spent last 5 hours getting work. appreciated. thanks!

please try following solution - works me in sharepoint environment

public listusersps(string searchstring) { .... windowsidentity identity = windowsidentity.getcurrent(); windowsimpersonationcontext ctx = null;  ctx = identity.impersonate();  runspaceconfiguration connectioninfo = runspaceconfiguration.create(); ... using (runspace rsp = runspacefactory.createrunspace(connectioninfo)) { ... } ... } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -