Wednesday, 12 October 2016

How to hide password while entering it on C# console application?


static void Main(string[] args)
        {
            string user;
            Console.Write("Enter your user name: ");
            user = Console.ReadLine();

            string pwd = "";
            Console.Write("Enter your password: ");
            ConsoleKeyInfo key;
            do
            {
                input = Console.ReadKey(true);

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

No comments:

Post a Comment