WindowsIdentity Diagnostics.Process

Z

ZUP_DUDE

I have a webservice that is using impersonation. The web service needs
to call a System.Diagnostics.Process to execute a batch file. I am
having problems executing the batch file. It works fine on my machine
but when I execute the process on the Server nothing seems to happen. I
think it is because I need to pass the credentials to the Process
thread. Is there any way to attach a WindowsIdentity to a
System.Diagnostics.Process?

An example of the process code is as follows.

WindowsIdentity winID = WindowsIdentity.GetCurrent();

// Would like to attach winID to the Process
Process process = new Process();
process.StartInfo.FileName = batchFileToRun; // Contains the batch
file name and path
process.StartInfo.Arguments = batchFileArgs; // Arguments to pass to
the batch file
process.Start();
process.WaitForExit();
process.Close();

Thanks in advance!
 
K

Kevin Yu [MSFT]

Hi ZUP_DUDE,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to start a new process with
different windows account. If there is any misunderstanding, please feel
free to let me know.

As far as I know, we don't have such property or method to achieve this
goal in current .NET framework. So we have to call some platform API to do
this. CreateProcessAsUser is the right thing to use. We can specify the
user information by setting the hToken argument. Here is more information
on this API:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/createprocessasuser.asp

We can declare the function as follows:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool CreateProcessAsUser(
IntPtr hToken,
string lpApplicationName,
[In] StringBuilder lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

Also, here is an example:

http://pinvoke.net/default.aspx/advapi32.CreateProcessAsUser

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Z

ZUP_DUDE

Thanks Kevin

Api calls are something new to me. I have taken a look at the links you
supplied me and it seems to be pretty complicated. I'll attempt to get
it working and will try to post back a simple code snippet for others.
 
K

Kevin Yu [MSFT]

You're welcome!

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top