Show Asterisks Instead Of Characters For Password Input In Console Application

Introduction

 
In this article you are going to learn about how to take password input and show asterisks instead of the entered characters in the console and also check the empty value.
 
Tools
 
Visual Studio
 
Targeted Audience
 
People with basic knowledge of C# and Windows Forms.
 
Steps
 
First of all open vVsual Studio and create a new project.
 
Show Asterisks Instead Of Characters For Password Input In Console Application 
 
Select console application .NET framework form the men, name your project as you want and click OK.
 
Show Asterisks Instead Of Characters For Password Input In Console Application 
 
After creating the project open Program.cs and there we will create a function which will take a parameter of the entered text which will be the text that will be shown to user. The function will listen to  the user every time he press a key. We will create a global variable “EnteredVal” which will contain the original text entered by the user. There will be a check which checks the pressed key  is neither backspace nor enter key, then it will store the value in global variable (EnteredVal) and write an asterisk in the console.
 
In the next case if the pressed key is backspace, it is the case when user presses any wrong key and wants correction, we will remove the last alphabet from the global variable (EnteredVal) and the last asterisk from the console.
 
In the second last case we will check if the user enters a empty value or not, if he tries to enter an empty one we will show an error and ask him to enter again.
 
In the last case we consider that the user enters his desired password and wants to proceed. Here we will show a confirmation message that he entered his password.
 
Paste the code below, under your Main() function.
 
C# Code
  1. static void CheckPassword(string EnterText)  
  2.         {  
  3.             try  
  4.             {  
  5.                 Console.Write(EnterText);  
  6.                 EnteredVal = "";  
  7.                 do  
  8.                 {  
  9.                     ConsoleKeyInfo key = Console.ReadKey(true);  
  10.                     // Backspace Should Not Work  
  11.                     if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)  
  12.                     {  
  13.                         EnteredVal += key.KeyChar;  
  14.                         Console.Write("*");  
  15.                     }  
  16.                     else  
  17.                     {  
  18.                         if (key.Key == ConsoleKey.Backspace && EnteredVal.Length > 0)  
  19.                         {  
  20.                             EnteredVal = EnteredVal.Substring(0, (EnteredVal.Length - 1));  
  21.                             Console.Write("\b \b");  
  22.                         }  
  23.                         else if (key.Key == ConsoleKey.Enter)  
  24.                         {  
  25.                             if (string.IsNullOrWhiteSpace(EnteredVal))  
  26.                             {  
  27.                                 Console.WriteLine("");  
  28.                                 Console.WriteLine("Empty value not allowed.");  
  29.                                 CheckPassword(EnterText);  
  30.                                 break;  
  31.                             }  
  32.                             else  
  33.                             {  
  34.                                 Console.WriteLine("");  
  35.                                 break;  
  36.                             }  
  37.                         }  
  38.                     }  
  39.                 } while (true);  
  40.             }  
  41.             catch (Exception ex)  
  42.             {  
  43.                 throw ex;  
  44.             }  
  45.         }  
This function will check the entered value and show asterisk on the console instead of the characters entered and keep the original entered value in a global variable which can be used further. It will also ask the user to enter again if he enters an empty value. Now in your Main() function create an “enterText” string variable and assign any input text you want to show the user. Call your function and pass the enterText as a parameter.
 
All your code will look something like given below.
 
C# Code
  1. using System;  
  2.   
  3. namespace ProjInputPasswordConsole  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static string EnteredVal = "";  
  8.         static void Main(string[] args)  
  9.         {  
  10.             string enterText = "Please enter password: ";  
  11.             CheckPassword(enterText);  
  12.             Console.ReadKey();  
  13.         }  
  14.         static void CheckPassword(string EnterText)  
  15.         {  
  16.             try  
  17.             {  
  18.                 Console.Write(EnterText);  
  19.                 EnteredVal = "";  
  20.                 do  
  21.                 {  
  22.                     ConsoleKeyInfo key = Console.ReadKey(true);  
  23.                     // Backspace Should Not Work  
  24.                     if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)  
  25.                     {  
  26.                         EnteredVal += key.KeyChar;  
  27.                         Console.Write("*");  
  28.                     }  
  29.                     else  
  30.                     {  
  31.                         if (key.Key == ConsoleKey.Backspace && EnteredVal.Length > 0)  
  32.                         {  
  33.                             EnteredVal = EnteredVal.Substring(0, (EnteredVal.Length - 1));  
  34.                             Console.Write("\b \b");  
  35.                         }  
  36.                         else if (key.Key == ConsoleKey.Enter)  
  37.                         {  
  38.                             if (string.IsNullOrWhiteSpace(EnteredVal))  
  39.                             {  
  40.                                 Console.WriteLine("");  
  41.                                 Console.WriteLine("Empty value not allowed.");  
  42.                                 CheckPassword(EnterText);  
  43.                                 break;  
  44.                             }  
  45.                             else  
  46.                             {  
  47.                                 Console.WriteLine("");  
  48.                                 break;  
  49.                             }  
  50.                         }  
  51.                     }  
  52.                 } while (true);  
  53.             }  
  54.             catch (Exception ex)  
  55.             {  
  56.                 throw ex;  
  57.             }  
  58.         }  
  59.     }  
  60. }  
Now run your code and see the output. Try to enter a value and it will show an asterisk.
 
Show Asterisks Instead Of Characters For Password Input In Console Application 
 
You can put the break point to see that the value is stored in the Global variable, which can be used further in saving in database etc.
 
Show Asterisks Instead Of Characters For Password Input In Console Application 
 
You can test this in different cases like pressing backspace or entering an empty value. So that’s all.
 

Conclusion

 
This article gives an brief introduction about how to make an password input in console application and how to put different checks on it.


Similar Articles