Saran
Saran

Reputation: 925

In c# How to Start new Process using logged on user?

Below is the scenario.

  1. Logged in to windows using user name "JOHN"
  2. Run the Windows application writtern in c# . This tool name is BootStrapper.exe. But this tool I executed using different user called "ALEX" using Run As feature.
  3. Boot strapper will show some button called "Launch Application". On clicking Launch executing Application.exe using Process class of c#. Note that i am not passing any user name and password. So Application.exe is also running under "ALEX" User.

How do I run the Application.exe under "JOHN" from Bootstrapper.exe even though it is started by "ALEX". Note that the password of the "JOHN" will not be known to Application.exe to impersonate JOHN user.

Upvotes: 0

Views: 815

Answers (2)

Andrey Bushman
Andrey Bushman

Reputation: 12476

I apologize for my bad English. Maybe I wrong understand you... Compile it, and copy result to "C:\test" directory. Now run it.

using System;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Reflection;
using System.IO;

namespace ConsoleApplication6 {
    class Program {

        unsafe static void Main(string[] args) {

            Process process = new Process();
            String dir = Path.GetDirectoryName(typeof(Program).Assembly.Location);

            String txtFile = Path.Combine(dir, "example.txt");
            if (!File.Exists(txtFile)) {
                StreamWriter sw = File.CreateText(txtFile);
                sw.Close();
                sw.Dispose();
            }

            ProcessStartInfo info = new ProcessStartInfo();

            info.Domain = "myDomainName";
            info.UserName = "userName";
            String pass = "userPassword";

            fixed (char* password = pass) {
                info.Password = new SecureString(password, pass.Length);
            }

            // Will be run notepad.exe
            info.FileName = Environment.ExpandEnvironmentVariables(@"%winDir%\NOTEPAD.EXE");
            // in notepad.exe will be open example.txt file.
            info.Arguments = txtFile;
            info.LoadUserProfile = false;
            info.UseShellExecute = false;
            info.WorkingDirectory = dir;

            process.StartInfo = info;
            process.Start();
        }
    }
}

Regards

Upvotes: 0

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

Host a WCF service in a process started by JOHN (maybe by putting it in the startup folder).

Call the WCF service from the ALEX process with a command telling what process to start.
Start the process from the WCF service and it will be running as JOHN.

Upvotes: 1

Related Questions