Windows Service doesn't start process with different credentials

Posted by Marcus on Stack Overflow See other posts from Stack Overflow or by Marcus
Published on 2013-11-04T09:50:36Z Indexed on 2013/11/04 9:53 UTC
Read the original article Hit count: 253

Filed under:

I have a Windows Service, running as a user, that should start several processes under different user credentials.

I'm using the following code to start a process:

        Dim winProcess As New System.Diagnostics.Process

        With winProcess
            .StartInfo.Arguments = "some_args"
            .StartInfo.CreateNoWindow = True
            .StartInfo.ErrorDialog = False
            .StartInfo.FileName = "C:\TEMP\ProcessFromService\ProcessFromService\bin\Debug\ProcessFromService.exe"
            .StartInfo.UseShellExecute = False
            .StartInfo.WindowStyle = ProcessWindowStyle.Hidden

            'Opgave WorkingDirectory kan soms tot problemen leiden, indien betreffende directory
            'niet bereikbaar (rechten) is voor opgegeven gebruiker.
            'Beter dus om deze niet op te geven.
            '.StartInfo.WorkingDirectory = My.Computer.FileSystem.SpecialDirectories.Temp

            .StartInfo.Domain = ""
            .StartInfo.UserName = "MyUserId"

            Dim strPassword As String = "MyPassword"
            Dim ssPassword As New Security.SecureString
            For Each chrPassword As Char In strPassword.ToCharArray
                ssPassword.AppendChar(chrPassword)
            Next
            .StartInfo.Password = ssPassword

            .Start()

        End With

The process is correctly started when I use the same credentials as of which the Windows Service is running under.

The process is not started, without any error, when I use different credentials.

In other words: If the Windows Service is running as UserA then I can start a process running as UserA. If the Windows Service is running as UserB then I can not start a process running as UserA.

I have created a test project in which I can reproduce this problem. If you put this project in C:\Temp then the used paths will be correct.

You can download this test project here: https://dl.dropboxusercontent.com/u/5391091/ProcessFromService.zip

NB: I hope this info is enough to explain it. If you need more info, please let me know and I will add it.

© Stack Overflow or respective owner

Related posts about vb.net