【问题标题】:Using ReadKey to have more than one "password"?使用 ReadKey 拥有多个“密码”?
【发布时间】:2016-07-19 15:30:11
【问题描述】:

所以我只是在使用控制台创建一个小程序,它会根据您键入的“密码”向您显示内容。因此,当您输入密码 POKEMON 时,它会将您带到数据库。但我想,如果您输入类似 PASSWORD 的内容,它会将您带到另一个数据库。我不知道该怎么做,我希望这就像说“如果他们输入 POKEMON 把他们带到这里,如果他们输入 PASSWORD 把他们带到这里”那么简单,但我遇到了真正的麻烦。感谢您的帮助。

namespace Films
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Jake. Security check: Please type the security code for a database.");
            while (Console.ReadKey().Key != ConsoleKey.P) {}
            while (Console.ReadKey().Key != ConsoleKey.O) {}
            while (Console.ReadKey().Key != ConsoleKey.K) {}
            while (Console.ReadKey().Key != ConsoleKey.E) {}
            while (Console.ReadKey().Key != ConsoleKey.M) {}
            while (Console.ReadKey().Key != ConsoleKey.O) {}
            while (Console.ReadKey().Key != ConsoleKey.N) {}
            { 
                Console.WriteLine("\n Passcode entered successfully.");
                while (Console.ReadKey().Key != ConsoleKey.O) { }
                while (Console.ReadKey().Key != ConsoleKey.K) { }
                {
                    Console.WriteLine("\n \n                       Loading Database");
                    Console.WriteLine("\n");
                    Console.WriteLine("Test");
                    Console.ReadKey();
                }
            }
        }
    }
}

【问题讨论】:

  • Console.ReadLine() 返回用户在按回车键之前输入的所有内容,这似乎是一种更好的方法。然后,您可以使用 switch() 检查文本并执行您需要的操作。
  • Baked in creds 从可执行文件中提取出来是微不足道的,因此毫无意义。

标签: c# passwords


【解决方案1】:

有一个方法叫Console.ReadLine()

ReadLine 方法从标准输入流中读取一行。

如果标准输入设备是键盘,则 ReadLine 方法会阻塞,直到用户按下 Enter 键。

在你的情况下,你会这样使用它:

Console.WriteLine("Hello Jake. Security check: Please type the security code for a database.");

// This will read all the characters until the user presses Enter
string password = Console.ReadLine();

switch (password)
{
    case "POKEMON":
    {
        // Load some database
        break;
    }
    case "PASSWORD":
    {
        // Load another database
        break;
    }
    default:
    {
        Console.WriteLine("Invalid password");
    }
}

【讨论】:

  • 工作就像一个魅力!非常感谢! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2016-07-13
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多