Masking a password argument in a console application

I need to write a console application in C # that enters a username and password as input parameters.

Example: mytool.exe --username foo --password bar

It is easy, but, of course, the password will be displayed on the screen as soon as the user types it. Security issue.

Is there any way to handle this in a safer way? The following, however, is not an option:

C:\>mytool.exe
Please enter username:
Please enter password:

The application must support the call without user interaction, for example. regularly administered cronjob.

+4
source share
2 answers

To comment on a SLaks comment:

stdin ( ). cron ,

MyConsoleProgram.exe < C:\SomeSecureFolder\password.txt

, @Ahmed, , .

, Password.txt, , .

+2

? :

if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
   pass += key.KeyChar;
   Console.Write("*");
}
else
{
   if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
   {
      pass = pass.Substring(0, (pass.Length - 1));
      Console.Write("\b \b");
   }
}

....

            int chr=0;
            string pass="";
            const int ENTER = 13;
            do
            {
                chr = Console.Read();
                Console.Write ('*');
                pass += (char)chr;
            }
            while (chr!=ENTER);
0

All Articles