views:

3476

answers:

3

I'm currently using a homegrown method to run a process as a different user in Vista, and I can't escape the feeling that's it hack-ish and less than ideal (in addition to the fact that it craps out UAC, crashing my app with a security exception, and forcing me to disable UAC altogether). My process consists of two projects (so two EXE files) - an "interface" and a "launch stub" - and here's the process:

  1. User has a shortcut that launches "Interface.exe notepad.exe"
  2. Interface.exe has a form that asks for the credentials they'd like to use
  3. Interace.exe uses ProcessStartInfo to create an instance of LaunchStub.exe (LS) as the new user
  4. LS uses ProcessStartInfo (with ShellExecute set to true) to launch the requested file, and since it's already running as the requested user, so is the new process.

The reason I have a two-step process is that I want users to be able to right-click on any file the OS has a default action for (.EXE, .SQL, .MSC, etc) and launch it, and ProcessStartInfo only supports that with "UseShellExecute" enabled, but that switch prevents me from using new credentials, so I can only do one at a time.

This causes a few problems - first, the user has to already exist on the computer, meaning they have to have logged in locally before. If there's no local profile for that user, the requested app will sometimes launch, but I get registry and profile exceptions because the application expects things to exist that don't yet (like an HKCU hive in the registry, which the user doesn't have because they've never logged in).

I know I should be able to just "Elevate" the rights of my application to the user they're requesting, launch my new process, and then undo the elevation, but I'm unable to find a good code sample for that, and I'm not sure that it would allow running as a completely different user. Does this all make sense? I just can't help feel like there's a better way to do this.


UPDATE: I just tried some Impersonation code I found online, but to no avail. When used in conjunction with ProcessStartInfo, it still seems to launch processes using my current login, not the one I've provided, even though I've activated impersonation using the provided credentials.

A: 

You may try to run runas from your app. Some examples and options here.

Sunny
+1  A: 
I'll give this a try and see what happens. If I can do it without having to use a second EXE as a stub, I'll be happy, and even more so if it doesn't crap out when UAC is enabled (I'll deal with a prompt). I'll be bakc after I've tried it.
rwmnau
A: 

Try this Module:

Module Impersonation

#Region "API Structures"
    <StructLayout(LayoutKind.Sequential)> _
      Public Structure PROCESS_INFORMATION
        Dim hProcess As System.IntPtr
        Dim hThread As System.IntPtr
        Dim dwProcessId As Integer
        Dim dwThreadId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
     Public Structure STARTUPINFO
        Dim cb As Integer
        Dim lpReserved As System.IntPtr
        Dim lpDesktop As System.IntPtr
        Dim lpTitle As System.IntPtr
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize As Integer
        Dim dwYSize As Integer
        Dim dwXCountChars As Integer
        Dim dwYCountChars As Integer
        Dim dwFillAttribute As Integer
        Dim dwFlags As Integer
        Dim wShowWindow As Short
        Dim cbReserved2 As Short
        Dim lpReserved2 As System.IntPtr
        Dim hStdInput As System.IntPtr
        Dim hStdOutput As System.IntPtr
        Dim hStdError As System.IntPtr
    End Structure
#End Region

#Region "API Constants"
    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2
    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000
    Private Const CREATE_NEW_CONSOLE As Integer = &H10
    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200
    Private Const LOGON_WITH_PROFILE As Integer = &H1
#End Region

#Region "API Functions"
    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _
        (ByVal lpUsername As String, _
         ByVal lpDomain As String, _
         ByVal lpPassword As String, _
         ByVal dwLogonFlags As Integer, _
         ByVal lpApplicationName As String, _
         ByVal lpCommandLine As String, _
         ByVal dwCreationFlags As Integer, _
         ByVal lpEnvironment As System.IntPtr, _
         ByVal lpCurrentDirectory As System.IntPtr, _
         ByRef lpStartupInfo As STARTUPINFO, _
         ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As System.IntPtr) As Integer

#End Region

    Public Sub RunProgram(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal Application As String, ByVal CommandLine As String)

        Dim siStartup As STARTUPINFO
        Dim piProcess As PROCESS_INFORMATION
        Dim intReturn As Integer

        If CommandLine Is Nothing OrElse CommandLine = "" Then CommandLine = String.Empty

        siStartup.cb = Marshal.SizeOf(siStartup)
        siStartup.dwFlags = 0

        intReturn = CreateProcessWithLogon(UserName, Domain, Password, LOGON_WITH_PROFILE, Application, CommandLine, _
        NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _
        IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

        If intReturn = 0 Then
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If

        CloseHandle(piProcess.hProcess)
        CloseHandle(piProcess.hThread)

    End Sub

End Module

Use Runprogram() To start your Program with user/pw y like. Programm means the .exe only, parameters ar written into "commandline"

DrFuture
How is this module different from the one posted earlier? I'm honestly asking - it looks pretty similar, so I just want to make sure I'm not missing something obvious that would make clearly superior. I haven't accepted an answer yet, but the previous code has been working for me until now.
rwmnau
yes its very similar, its only a second function written rounded the apifunktion CreateProcessWithLogonW API - ready_to_use with errorhandling and successfeedback ...
DrFuture