问题
I'm trying to create a console application to replace a batch file. The batch file prompted for a user and ran the following code...
RUNAS /user:USA\%usr% "C:\Program Files\Internet Explorer\iexplore.exe %ServerPath%/%AppName%"
How can I translate this to C# code? I'm basically using the code below. I declare a User Name and a Path, but it always launches IE with my windows login. Am I using Verb incorrectly? Do I need to include a password, and if so, how?
string sPath = ServerPath
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore.exe");
startInfo.Verb = @"runas /user:USA\" + sUser;
startInfo.Arguments = sPath;
startInfo.UseShellExecute = false;
Process.Start(startInfo);
回答1:
function SecureString MakeSecureString(string text)
{
SecureString secure = new SecureString();
foreach (char c in text)
{
secure.AppendChar(c);
}
return secure;
}
function void RunAs(string path, string username, string password)
{
ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
}
RunAs(APPLICATION, USERNAME, PASSWORD);
Props to fraser chapman's blog
来源:https://stackoverflow.com/questions/4495596/how-can-i-run-ie-with-different-users-and-specify-a-url