Microsoft KB Archive/889251

= How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages =

Article ID: 889251

Article Last Modified on 6/2/2005

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-





IN THIS TASK

 * INTRODUCTION
 * Description of the technique
 * Steps to spawn a process
 * REFERENCES



INTRODUCTION
This step-by-step article describes how to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages. To spawn a process that runs under the context of the impersonated user, you cannot use the System.Diagnostics.Process.Start method. This is because in ASP.NET, impersonation is performed at the thread level and not at the process level. Therefore, any process that you spawn from ASP.NET will run under the context of the ASP.NET worker process and not under the impersonated context.

back to the top

Description of the technique
To work around this issue, you must make platform invoke (P/Invoke) calls to the following Win32 APIs:
 * The CreateProcessAsUser function creates a process under the identity of the security token that is specified in one of the parameters. However, the call to the CreateProcessAsUser function requires a primary token. Therefore, you must convert the impersonated token to a primary token.
 * The DuplicateTokenEx function converts an impersonated token to a primary token. The call to the CreateProcessAsUser function uses this primary token.

Note For the call to the CreateProcessAsUser function to work, the following security user rights must be assigned to the impersonated user:
 * On a computer that is running Microsoft Windows 2000, assign the Replace a process level token user right and the Increase quotas user right to the impersonated user.
 * On a computer that is running Microsoft Windows Server 2003 or Microsoft Windows XP, assign the Replace a process level token user right to the impersonated user.

back to the top

Steps to spawn a process
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To spawn a process that runs under the context of the impersonated user, follow these steps:  In Microsoft Visual Studio .NET 2003, create a new Visual C# ASP.NET Web application, and then name the application Q889251. Open the WebForm1.aspx file in code view.  In the WebForm1.aspx file, add the following statements to the using block of code. using System.Runtime.InteropServices; using System.Security.Principal;   Locate the line of code that looks similar to the following line of code. public class WebForm1 : System.Web.UI.Page   After the line of code that is identified in step 4, add the following code. [StructLayout(LayoutKind.Sequential)] public struct STARTUPINFO {           public int cb; public String lpReserved; public String lpDesktop; public String lpTitle; public uint dwX; public uint dwY; public uint dwXSize; public uint dwYSize; public uint dwXCountChars; public uint dwYCountChars; public uint dwFillAttribute; public uint dwFlags; public short wShowWindow; public short cbReserved2; public IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; }

[StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION {           public IntPtr hProcess; public IntPtr hThread; public uint  dwProcessId; public uint  dwThreadId; }

[StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES {           public int    Length; public IntPtr lpSecurityDescriptor; public bool  bInheritHandle; }

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;CloseHandle&quot;, SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public extern static bool CloseHandle(IntPtr handle);

[DllImport(&quot;advapi32.dll&quot;, EntryPoint=&quot;CreateProcessAsUser&quot;, SetLastError=true, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)] public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,            ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,            String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

[DllImport(&quot;advapi32.dll&quot;, EntryPoint=&quot;DuplicateTokenEx&quot;)] public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,            ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,            int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);  In Solution Explorer, right-click WebForm1.aspx, and then click Open. The Web form opens in design view. On the View menu, click HTML Source.</li>  Replace all the existing code in the HTML Source window with the following code. <%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;Q889251.WebForm1&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; > <HTML> <HEAD> WebForm1 <meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 7.0&quot;> <meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;> <meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;> <meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;> </HEAD> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <P> </P> <P>Enter Path of process to be run (with relevant parameters) <asp:TextBox id=&quot;TextBox1&quot; runat=&quot;server&quot;></asp:TextBox></P> <P> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;CreateProcess&quot;></asp:Button> </P> <P> <asp:Label id=&quot;Label1&quot; runat=&quot;server&quot;>Status:</asp:Label></P> <P> <asp:Label id=&quot;Label2&quot; runat=&quot;server&quot;>Impersonated Identity:</asp:Label></P> </HTML> </li> On the View menu, click Design.</li> Double-click CreateProcess. The Button1_Click method is inserted into the code, and the code appears in the content pane.</li>  Replace the existing Button1_Click method with the following code. private void Button1_Click(object sender, System.EventArgs e)       { IntPtr Token = new IntPtr(0); IntPtr DupedToken = new IntPtr(0); bool     ret; Label2.Text+=WindowsIdentity.GetCurrent.Name.ToString;

SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES; sa.bInheritHandle      = false; sa.Length              = Marshal.SizeOf(sa); sa.lpSecurityDescriptor = (IntPtr)0; Token = WindowsIdentity.GetCurrent.Token;

const uint GENERIC_ALL = 0x10000000;

const int SecurityImpersonation = 2; const int TokenType = 1;

ret = DuplicateTokenEx(Token, GENERIC_ALL, ref sa, SecurityImpersonation, TokenType, ref DupedToken); if (ret == false) Label1.Text +=&quot;DuplicateTokenEx failed with &quot; + Marshal.GetLastWin32Error; else Label1.Text+= &quot;DuplicateTokenEx SUCCESS&quot;;

STARTUPINFO si         = new STARTUPINFO; si.cb                  = Marshal.SizeOf(si); si.lpDesktop           = &quot;&quot;; string commandLinePath; commandLinePath = TextBox1.Text;

PROCESS_INFORMATION pi = new PROCESS_INFORMATION; ret = CreateProcessAsUser(DupedToken,null,commandLinePath, ref sa, ref sa, false, 0, (IntPtr)0, &quot;c:\\&quot;, ref si, out pi); if (ret == false) Label1.Text +=&quot;CreateProcessAsUser failed with &quot; + Marshal.GetLastWin32Error; else {               Label1.Text +=&quot;CreateProcessAsUser SUCCESS. The child PID is&quot; + pi.dwProcessId;

CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }

ret = CloseHandle(DupedToken); if (ret == false) Label1.Text+=Marshal.GetLastWin32Error; else Label1.Text+=&quot;CloseHandle SUCCESS&quot;; } </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> In the WebForm1 page, type the path of a process that you want to start. For example, type \Windows\Notepad.exe .</li> Click CreateProcess.</li></ol>

back to the top

<div class="references_section">