我确实找到了这个,但如果返回代码为NO ERROR
,无法弄清楚如何以该用户身份运行该流程 public bool PromptForPassword()
{
// Setup the flags and variables
StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder();
CREDUI_INFO credUI = new CREDUI_INFO();
credUI.cbSize = Marshal.SizeOf(credUI);
credUI.pszCaptionText = "Run As";
credUI.pszMessageText = "Running Software As An Admin";
bool save = false;
//bool save = true;
//CREDUI_FLAGS flags = CREDUI_FLAGS.ALWAYS_SHOW_UI | CREDUI_FLAGS.GENERIC_CREDENTIALS | CREDUI_FLAGS.DO_NOT_PERSIST | CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX | CREDUI_FLAGS.PERSIST;
CREDUI_FLAGS flags = CREDUI_FLAGS.ALWAYS_SHOW_UI | CREDUI_FLAGS.GENERIC_CREDENTIALS | CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX | CREDUI_FLAGS.REQUEST_ADMINISTRATOR;
// Prompt the user
CredUIReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, this.serverName, IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);
_user = userID.ToString();
_securePassword = new SecureString();
for (int i = 0; i < userPassword.Length; i++)
{
_securePassword.AppendChar(userPassword[i]);
}
return (returnCode == CredUIReturnCodes.NO_ERROR);
}
如何使用它以不同的用户身份运行该流程?我必须使用 CredUIPromptForCredentials 来询问Windows凭据。所以我不认为我可以将这些值传递给下面的代码。
System.Security.SecureString ssPwd = new System.Security.SecureString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "filename";
proc.StartInfo.Arguments = "args...";
proc.StartInfo.Domain = "domainname";
proc.StartInfo.UserName = "username";
string password = "user entered password";