Итак, у меня все работает с обычной строкой, но теперь мне нужно сделать это в securestring
с паролем маскировки.
Мой код выглядит так:
Console.WriteLine("Input username:");
string username = Console.ReadLine();
Console.WriteLine("Input password:");
SecureString password = new SecureString();
password = Classes.Functions.GetPassword();
Classes.Functions.runProcRasdial("VPN_Arta", username, password);
Console.Clear();
Console.WriteLine("VPN Connected.");
Метод вызова rasdial:
public static Process runProcRasdial(string VPNName, string username, SecureString password)
{
ProcessStartInfo psi = new ProcessStartInfo("cmd")
{
RedirectStandardInput = true,
RedirectStandardOutput = false,
UseShellExecute = false
};
var proc = new Process()
{
StartInfo = psi,
EnableRaisingEvents = true,
};
proc.Start();
proc.StandardInput.WriteLine("rasdial {0} {1} {2}", VPNName, username, password);
proc.StandardInput.WriteLine("exit");
proc.WaitForExit();
return proc;
}
Способ маскировки пароля:
//mask password
public static SecureString GetPassword()
{
var pwd = new SecureString();
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.RemoveAt(pwd.Length - 1);
Console.Write("\b \b");
}
}
else if (i.KeyChar != '\u0000' ) // KeyChar == '\u0000' if the key pressed does not correspond to a printable character, e.g. F1, Pause-Break, etc
{
pwd.AppendChar(i.KeyChar);
Console.Write("*");
}
}
return pwd;
}
Проблема в том, что я не получаю никакой ошибки, все выглядит просто отлично. Но я думаю, что будет проблема с функцией маскировки пароля, потому что она не принимает правильный пароль, и я не знаю.
Ребята, есть идеи?
Спасибо
John