Run the command or application in the Security context of specified user

Use below code when you want to run "command or application" with "Arguments" as a particular user. Suppose you want to run command say compmgmt.msc or any command or application as a user domain\username then change the password and username in below program. Currently this program runs a specified application in the security context of the current logged on user. This program will also display output/error of the program. You can convert this program so that it can take all argum

Complete C# Program
-------------------

using System;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Remoting;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace Target_Process
{
class Program
{
static void Main(string[] args)
{
try
{
CreateProcessWithLoadProfile("Password","Command or Application Path","Arguments");
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex);
}
}

private static void CreateProcessWithLoadProfile(
string password,
string commandPath,
string commandArguments)
{
Process MyProcess = new Process();
MyProcess.StartInfo.FileName = "cmd.exe";
MyProcess.StartInfo.Arguments = " /c " + commandPath + " " + commandArguments;
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;
MyProcess.StartInfo.LoadUserProfile = true;
MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.StartInfo.RedirectStandardError = true;
MyProcess.StartInfo.RedirectStandardInput = true;

// Set our event handler to asynchronously read the sort output.
MyProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
MyProcess.ErrorDataReceived += new DataReceivedEventHandler(ProcessErrorHandler);

// get the domain and user name parts of the current
// windows identity
Match identity_match = Regex.Match(
WindowsIdentity.GetCurrent().Name,
@"^([^\\]+)\\(.+)$");
// domain name
string dn = identity_match.Groups[1].Value;
// user name
string un = identity_match.Groups[2].Value;

// only set the domain if it is an actual domain and
// not the name of the local machine, i.e. a local account
// invoking sudo
if (!Regex.IsMatch(dn,
Environment.MachineName, RegexOptions.IgnoreCase))
{
MyProcess.StartInfo.Domain = dn;
}

MyProcess.StartInfo.UserName = un;

// transform the plain-text password into a
// SecureString so that the ProcessStartInfo class
// can use it
MyProcess.StartInfo.Password = new System.Security.SecureString();
for (int x = 0; x < password.Length; ++x)
MyProcess.StartInfo.Password.AppendChar(password[x]);

try
{
MyProcess.Start();
}
catch (Exception e)
{
throw new Exception(null, e);
}
// Start the asynchronous read of the sort output stream.
MyProcess.BeginOutputReadLine();
MyProcess.BeginErrorReadLine();
MyProcess.WaitForExit();
MyProcess.Close();

}

private static void ProcessOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
Console.WriteLine(outLine.Data);
}

private static void ProcessErrorHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
Console.WriteLine(outLine.Data);
}
}
}

Regards,
Megha
By Perry    Popularity  (2534 Views)