【发布时间】:2017-03-06 09:46:08
【问题描述】:
我是一个新手程序员。我需要编写桌面应用程序,用户输入用户名和密码。单击按钮后调用方法 Start 类 Process。不幸的是,出现错误消息System.ComponentModel.Win32Exception,用户名或密码不正确。
我的代码:
//Download data
String user = this.textBoxUser.Text;
String pass = this.textBoxPassword.Text;
//Password
SecureString secret = SecureStringConverter.ConvertToSecureString(pass);
//Webbrowser
string file = "chrome.exe";
string domain = @"http://localhost:62074/";
//Process start
Process proc = new Process();
Microsoft.Win32.RegistryKey subKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
String DefaultBrowser = subKey.GetValue(null).ToString();
if (DefaultBrowser != null)
{
int startIndex = DefaultBrowser.IndexOf("\"") + 1;
int endIndex = DefaultBrowser.IndexOf("\"", startIndex);
string RegDefaultBrowserPath = DefaultBrowser.Substring(startIndex, endIndex - startIndex);
proc.StartInfo.FileName = RegDefaultBrowserPath;
proc.StartInfo.Arguments = domain;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.LoadUserProfile = false;
proc.StartInfo.UserName = user;
proc.StartInfo.Password = secret;
proc.Start();
}
还有这个方法将字符串转换为SecureString
public static class SecureStringConverter
{
public static SecureString ConvertToSecureString(string password)
{
if (password == null)
throw new ArgumentNullException("password");
unsafe
{
fixed (char* passwordChars = password)
{
var securePassword = new SecureString(passwordChars, password.Length);
securePassword.MakeReadOnly();
return securePassword;
}
}
}
}
【问题讨论】:
-
请说明您的意图。您是否尝试将用户凭据转发到 Chrome 浏览器?
-
是的。我尝试将用户凭据转发到 Chrome 浏览器以在 Web 应用程序中对他进行身份验证。
-
为什么需要以其他用户身份启动 Chrome?
标签: c# asp.net visual-studio google-chrome process.start