first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# Code: Gathering Credentials from the Console; NetworkCredential; ConsoleKeyInfo

Console.WriteLine( "Enter username:" ); string userName = Console.ReadLine();

Console.WriteLine( "\\nEnter password:" ); StringBuilder password = new StringBuilder();

ConsoleKeyInfo info = Console.ReadKey( true ); while ( info.Key != ConsoleKey.Enter ) { if ( info.Key != ConsoleKey.Backspace ) { password.Append( info.KeyChar ); info = Console.ReadKey( true ); } else if ( info.Key == ConsoleKey.Backspace ) { if ( password.Length > 0 ) password.Remove( password.Length - 1, 1 ); info = Console.ReadKey( true ); } } for ( int i = 0; i < password.Length; i++ ) Console.Write( "*" ); Console.WriteLine();

string domain = String.Empty; string[] stringArray = userName.Split( new char[] { '\\\\' } ); if ( stringArray.Length > 1 ) { domain = stringArray[0]; userName = stringArray[1]; } else { domain = Environment.MachineName; }

NetworkCredential cred = new NetworkCredential( userName, password.ToString(), domain );

/* For more information see:

“How to: Authenticate with a User Name and Password”
http://msdn2.microsoft.com/en-us/library/ms733131.aspx

“ConsoleKeyInfo Structure”
http://msdn2.microsoft.com/en-us/library/system.consolekeyinfo.aspx

*/

mod date: 2007-12-06T22:39:30.000Z