我确实找到了这一点,但如果返回码为 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";