Hie friends, consider a situation where you are developing a console login application.In this application user enters his Username and password.
Username: Admin
Password: ******* (Hidden Pasword)
Above is the example of secure login. But if you are developing this application in c# console, C# doesn't have such method which could hide you password.
Here is the code through which you can ask user to input a string secretly.
Snippet 1:
private string GetPassword() { string Password = ""; ConsoleKeyInfo MyKey = Console.ReadKey(true); Console.Write("*"); while (!(MyKey.Key == ConsoleKey.Enter)) { Password += MyKey.KeyChar; MyKey = Console.ReadKey(true); if (MyKey.Key == ConsoleKey.Backspace) { if(!string.IsNullOrEmpty(Password)) { Console.Write("\b"); Password.Substring(0, Password.Length - 1); } } if (MyKey.Key != ConsoleKey.Backspace && MyKey.Key != ConsoleKey.Enter) { Console.Write("*"); } } return Password; }
How to use in program?
Snippet:2
public void UserLogin() { string uname,pwd; Console.Write("Username: "); uname = Console.ReadLine(); Console.Write("Password: "); pwd = GetPassword(); }
Hope this could help you.
Thanks!!