Password Masking in C# Console Application

Initiotech
Posted by Initiotech under C# category on | Views : 10051
The below Code can be used to display "*" instead of the users typed characters.
Just like the Password Text Box in Winforms.

In the Main Method Type the Following Code

ConsoleKeyInfo key;string pass = "";
do{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace) // Backspace Should Not Work
{
pass += key.KeyChar;
Console.Write("*");
}
} while (key.Key != ConsoleKey.Enter); // Stops Receving Keys Once Enter is Pressed
Console.WriteLine();
Console.WriteLine("The Password You entered is : " + pass);


Regards
Hefin Dsouza

Comments or Responses

Posted by: Neeks on: 9/22/2009 Level:Bronze | Status: [Member]
Great code

Login to post response