读取密码并确认并在循环中重试时出错

时间:2015-05-16 23:45:09

标签: c# validation passwords

编辑:我遇到的问题是我输入了一个不正确的密码来确认以前的密码,因为这是第一次尝试,所以它有效。最多3次尝试。在第二次和第三次尝试期间,即使密码正确,也会说无效密码。 我不想那个

我知道我错过了一些非常微不足道的东西,但我无法找到它。这是代码:

class Program
{
    static void Main(string[] args)
    {
        string username = "";
        string pass = "";
        string confirmPass = "";
        Console.Write("Enter username: ");
        username = Console.ReadLine();
        Console.WriteLine();
        Console.Write("Enter your password: ");
        ConsoleKeyInfo key;

        do
        {
            key = Console.ReadKey(true);

            // Backspace Should Not Work
            if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
            {
                pass += key.KeyChar;
                Console.Write("*");
            }
            else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
            {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
            }

        // Stops Receving Keys Once Enter is Pressed
        } while (key.Key != ConsoleKey.Enter);

        Console.WriteLine("");
        Console.WriteLine("Confirm your password: ");
        ConsoleKeyInfo confirmKey;

        int retryCount = 3;

        string finalPass = "";
        do{


            confirmKey = Console.ReadKey(true);

            // Backspace Should Not Work
            if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
            {
                confirmPass += confirmKey.KeyChar;
                Console.Write("*");
            }
            else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
            {
                confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                Console.Write("\b \b");
            }
            //

            // Stops Receving Keys Once Enter is Pressed
        } while ((confirmKey.Key != ConsoleKey.Enter));

        //Console.WriteLine("");
        //string confirmPass = "";

        do
        {
            if (confirmPass != pass)
            {

                Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");

                do
                {


                   confirmKey = Console.ReadKey(true);

                    // Backspace Should Not Work
                    if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
                    {
                        confirmPass += confirmKey.KeyChar;
                        Console.Write("*");
                    }
                    else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
                    {
                        confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                        Console.Write("\b \b");
                    }
                    //

                    // Stops Receving Keys Once Enter is Pressed
                } while ((confirmKey.Key != ConsoleKey.Enter));
                retryCount--;

            }

            else if (confirmPass == pass)
            {
                Console.WriteLine("Enter password to log in :");
                finalPass = Console.ReadLine();
                if (finalPass == pass)
                {
                    Console.WriteLine("Login Successful. Welcome " + username + "!");
                    Console.WriteLine();
                    Console.WriteLine("Test Successful. Press Enter to quit.");
                }
            }
            if (retryCount == 0)
            {
                Console.WriteLine("Exceeded number of tries!!");
                Console.ReadLine();
            }
        } while (confirmPass != pass && retryCount != 0);
    }
}

2 个答案:

答案 0 :(得分:0)

根据您的代码,这就是我想出的:

public static void Main(string[] args)
{
    string username = "";
    string pass = "";
    string confirmPass = "";
    Console.Write("Enter username: ");
    username = Console.ReadLine();
    Console.WriteLine();
    Console.Write("Enter your password: ");
    ConsoleKeyInfo key;

    do
    {
        key = Console.ReadKey(true);

        // Backspace Should Not Work
        if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
        {
            pass += key.KeyChar;
            Console.Write("*");
        }
        else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
        {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
        }
        // Stops Receving Keys Once Enter is Pressed
    } while (key.Key != ConsoleKey.Enter);

    Console.WriteLine("");
    Console.WriteLine("Confirm your password: ");
    ConsoleKeyInfo confirmKey;

    int retryCount = 3;

    string finalPass = "";
    do
    {
        confirmKey = Console.ReadKey(true);
        // Backspace Should Not Work
        if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
        {
            confirmPass += confirmKey.KeyChar;
            Console.Write("*");
        }
        else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
        {
            confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
            Console.Write("\b \b");
        }
        // Stops Receving Keys Once Enter is Pressed
    } while ((confirmKey.Key != ConsoleKey.Enter));

    do
    {
        if (confirmPass != pass)
        {
            confirmPass = "";
            Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");

            do
            {
                confirmKey = Console.ReadKey(true);

                // Backspace Should Not Work
                if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
                {
                    confirmPass += confirmKey.KeyChar;
                    Console.Write("*");
                }
                else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
                {
                    confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                    Console.Write("\b \b");
                }
                // Stops Receving Keys Once Enter is Pressed
            } while ((confirmKey.Key != ConsoleKey.Enter));
            retryCount--;
        }
        else if (confirmPass == pass)
        {
            Console.WriteLine("Enter password to log in :");
            finalPass = Console.ReadLine();
            if (finalPass == pass)
            {
                Console.WriteLine("Login Successful. Welcome " + username + "!");
                Console.WriteLine();
                Console.WriteLine("Test Successful. Press Enter to quit.");
                Console.ReadLine();
                break;
            }
        }
        if (retryCount == 0)
        {
            Console.WriteLine("Exceeded number of tries!!");
            Console.ReadLine();
        }
    } while (retryCount != 0);
}

基本上我做了以下更改:

  1. 当您输入最后一个do循环并输入第一个if语句时,我设置了confirmPass = "";。这就是您的代码最初没有执行此操作的原因,因为您将 confirmPass附加到原始代码中。因此,如果他们输入password,然后输入password2,则重试他们输入passwordconfirmPasspassword2password
  2. 更改此设置后,流量无法正常运行,因此我从confirmPass != pass &&中删除了while位。这使您可以进入该循环中的elseif部分。
  3. 尽管如此,我认为这有点不对,因为一旦你点击elseif它就会无限循环。所以我告诉break里面elseif 杀死那个do/while循环。
  4. 如果此解决方案不正确,请澄清您的要求,以便我们更好地为您提供帮助。

答案 1 :(得分:0)

您的程序相当复杂,这就是为什么发现任何错误会更加困难的原因。您应该真正学习如何使用子例程(即函数),这样您就不必重复一遍又一遍地执行相同操作的代码块(例如读取密码)。

如果我们稍微重构一下代码,可以将这些重复的部分移到一个名为ReadPassword的方法中,如下所示:

public static string ReadPassword()
{
    var pass = "";
    while (true)
    {
        var key = Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
            // Stop Receving Keys Once Enter is Pressed
            break;

        // Backspace Should Not Work
        if (key.Key != ConsoleKey.Backspace)
        {
            pass += key.KeyChar;
            Console.Write("*");
        }
        else if (pass.Length > 0)
        {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
        }
    }
    return pass;
}

并用它来替换用于读取密码的所有do ... while循环,而不更改代码中的任何其他内容(因此错误仍然存​​在):

public static void Main(params string[] args)
{
    string username = "";
    string pass = "";
    string confirmPass = "";
    Console.Write("Enter username: ");
    username = Console.ReadLine();
    Console.WriteLine();
    Console.Write("Enter your password: ");
    pass = ReadPassword(); // changed to function call

    Console.WriteLine("");
    Console.WriteLine("Confirm your password: ");
    int retryCount = 3;
    string finalPass = "";
    confirmPass = ReadPassword(); // changed to function call

    do
    {
        if (confirmPass != pass) // <==== this is performed before the "else"
        {
            Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");
            confirmPass = ReadPassword(); // changed to function call
            retryCount--;
        }
        else if (confirmPass == pass) // <==== "else": bug
        {
            // Bug: it will only get here, if the confirmed password 
            // was entered correctly the first time
            Console.WriteLine("Enter password to log in :");
            finalPass = Console.ReadLine();
            if (finalPass == pass)
            {
                Console.WriteLine("Login Successful. Welcome " + username + "!");
                Console.WriteLine();
                Console.WriteLine("Test Successful. Press Enter to quit.");
            }
            // else <==== now what?
        }
        if (retryCount == 0)
        {
            Console.WriteLine("Exceeded number of tries!!");
            Console.ReadLine();
        }
    } while (confirmPass != pass && retryCount != 0);
}

现在,错误变得更容易被发现。我在评论中使用<===突出显示了它们。我会留给你弄清楚出了什么问题,以及你如何解决它。