Accepting input as a password from Console in C# 
Author Message
 Accepting input as a password from Console in C#

I am running a C# application in which I accept arguments from the
user from command line. One of the arguments is password. Is there any
way that when user enters the password, I don't display (either keep
it invisible or display it with "*")? Like in case of LINUX or UNIX,
when we login, the password is not echoed back to the user. How could
this be done in C#?


Thu, 24 Mar 2005 07:49:07 GMT  
 Accepting input as a password from Console in C#
You have to use [heavy] PInvoke to the Win32 API:
   http://msdn.microsoft.com/library/en-us/dllproc/base/character_mode_a...

 C#
 http://www.gotdotnet.com/userarea/filedetails.aspx?FileName=ConsoleEx...
 http://www.mentalis.org/soft/class.qpx?id=2
 http://staff.develop.com/woodring/dotnet/#DirectConsole
 http://support.microsoft.com/default.aspx?scid=kb;en-us;Q319257

I guess it is related to SetConsoleMode :
  ENABLE_ECHO_INPUT ENABLE_LINE_INPUT
and reading each single key character,
then writing a * to screen.

--
 Thomas Scheidegger - MVP .NET - 'NETMaster'
 http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/

Quote:

> I am running a C# application in which I accept arguments from the
> user from command line. One of the arguments is password. Is there any
> way that when user enters the password, I don't display (either keep
> it invisible or display it with "*")? Like in case of LINUX or UNIX,
> when we login, the password is not echoed back to the user. How could
> this be done in C#?



Thu, 24 Mar 2005 20:08:20 GMT  
 Accepting input as a password from Console in C#
Only using PInvoke.

Here's a small console sample....

using System;
using System.Text;
using System.Runtime.InteropServices;

class App {
    static void Main(string[] args) {

        string secret  = GetSecret();
  Console.WriteLine(secret);
    }

    [DllImport("kernel32", SetLastError=true)]
    static extern IntPtr GetStdHandle(IntPtr whichHandle);
    [DllImport("kernel32", SetLastError=true)]
    static extern bool GetConsoleMode(IntPtr handle, out uint mode);
    [DllImport("kernel32", SetLastError=true)]
    static extern bool SetConsoleMode(IntPtr handle, uint mode);

    static readonly IntPtr STD_INPUT_HANDLE = new IntPtr(-10);
 const int ENABLE_LINE_INPUT = 2;
    const uint ENABLE_ECHO_INPUT = 4;

    static string GetSecret() {
        // turn off console echo
        IntPtr hConsole = GetStdHandle(STD_INPUT_HANDLE);
        uint oldMode;
        if (!GetConsoleMode(hConsole, out oldMode))
  {
            throw new ApplicationException("GetConsoleMode failed");
        }
        uint newMode = oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
        if (!SetConsoleMode(hConsole, newMode)) {
            throw new ApplicationException("SetConsoleMode failed");
        }
  int i;
  StringBuilder secret = new StringBuilder();
        Console.Write("Enter password  :");
  while (true)
  {
   i = Console.Read ();
   if (i == 13)     // break when <return>
    break;
   secret.Append((char) i);
   Console.Write ("*");
  }

        Console.WriteLine();
        // restore console echo and line input mode
        if (!SetConsoleMode(hConsole, oldMode)) {
            throw new ApplicationException("SetConsoleMode failed");
        }
        return secret.ToString();
    }

Quote:
}

Willy.
Quote:

> I am running a C# application in which I accept arguments from the
> user from command line. One of the arguments is password. Is there any
> way that when user enters the password, I don't display (either keep
> it invisible or display it with "*")? Like in case of LINUX or UNIX,
> when we login, the password is not echoed back to the user. How could
> this be done in C#?



Fri, 25 Mar 2005 00:14:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Please Help: Accepting Characters as Input

2. Please Help: Accepting Characters as Input

3. How to write a C/C++ program that can accept input from a pipe

4. Some Expert level complications, Re: To accept input from PIPE by a program

5. To accept input from PIPE by a program

6. RLE code needs to accept array input

7. Accepting input from cursor keys...

8. Accepting input from cursor keys...

9. Can compiler accept c-source from standard input?

10. Window Not Accepting Accelerator Input

11. Accepting input without echo...

12. Need Grid Control that accepts user inputs (like spreadsheet)

 

 
Powered by phpBB® Forum Software