The problem is quite simple: You add every character to your password, including the ENTER key at the end.
So, when you do the password check, you compare "i" with "i\r" which will never match.
Two ways round this:
1) Add the '\r' character to your password. Nasty, brute-force-and-ignorance approach.
2) Re-work your loop:
ConsoleKeyInfo key = Console.ReadKey(true);
while (key.Key != ConsoleKey.Enter)
{
if (key.Key != ConsoleKey.Backspace)
{
password += key.KeyChar;
Console.Write("*");
}
else
{
if (password.Length > 0)
{
password = password.Substring(0, (password.Length - 1));
Console.Write("\b \b");
}
}
key = Console.ReadKey(true);
}
You could have found this yourself, very easily. Since the problem is that the two strings don't appear to match, when you think they should, you need to find out why not.
So. put a breakpoint on the line:
if (password != pass)
The execution of your program will then stop there, and you can look at (and change if you need to) the values "password" and "pass". You can then start looking back in your code to find the source of the problem.
But while you are here:
if (password != pass)
{
...
}
else if (password == pass)
{
...
}
else
...
Doesn't need to be that complex. Since password either is or isn't equal to pass, it is normal to just write:
if (password != pass)
{
...
}
else
{
...
}
(Unless you are feeling really paranoid and suspect that a bool has three states: "true", "false" and "quantum". It doesn't, in .NET. Honest.)