-
-
Save rqx110/2ef5ee057a1ad112e391 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Gets the console secure password. | |
/// </summary> | |
/// <returns></returns> | |
private static SecureString GetConsoleSecurePassword( ) | |
{ | |
SecureString pwd = new SecureString( ); | |
while ( true ) | |
{ | |
ConsoleKeyInfo i = Console.ReadKey( true ); | |
if ( i.Key == ConsoleKey.Enter ) | |
{ | |
break; | |
} | |
else if ( i.Key == ConsoleKey.Backspace ) | |
{ | |
pwd.RemoveAt( pwd.Length - 1 ); | |
Console.Write( "\b \b" ); | |
} | |
else | |
{ | |
pwd.AppendChar( i.KeyChar ); | |
Console.Write( "*" ); | |
} | |
} | |
return pwd; | |
} | |
/// <summary> | |
/// Gets the console password. | |
/// </summary> | |
/// <returns></returns> | |
private static string GetConsolePassword( ) | |
{ | |
StringBuilder sb = new StringBuilder( ); | |
while ( true ) | |
{ | |
ConsoleKeyInfo cki = Console.ReadKey( true ); | |
if ( cki.Key == ConsoleKey.Enter ) | |
{ | |
Console.WriteLine( ); | |
break; | |
} | |
if ( cki.Key == ConsoleKey.Backspace ) | |
{ | |
if ( sb.Length > 0 ) | |
{ | |
Console.Write( "\b\0\b" ); | |
sb.Length--; | |
} | |
continue; | |
} | |
Console.Write( '*' ); | |
sb.Append( cki.KeyChar ); | |
} | |
return sb.ToString( ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment