c# - 两个字符串变量与返回 bool 值的私有(private)字典进行比较c#

标签 c#

我试图创建两个名为用户名和密码的字符串变量,并返回一个名为“authentiated”的 bool 值。我试图将用户名和密码字符串与私有(private)字典的内容进行比较。如果用户名和密码匹配,我希望将 bool 值设置为 true。然而,我是 C# 新手,真的不知道该去哪里。任何帮助,将不胜感激。以下是我已经拥有的。 `

private Dictionary<string, string> dictionary = new Dictionary<string, string>();

        public Authenticator()
        {
            dictionary.Add("username1", "password1");
            dictionary.Add("username2", "password2");
            dictionary.Add("username3", "password3");
            dictionary.Add("username4", "password4");
            dictionary.Add("username5", "password5");
        }


        public Boolean Authenticate(Boolean authenticated)
        {
            //get user input 
            Console.WriteLine("Please enter a username");
            string inputUsername = Console.ReadLine();

          var auth1 = from entry in dictionary
                             where entry.Key == " ";

            Console.WriteLine("Please enter your password");
            string inputPassword = Console.ReadLine();

            var auth2 = from entry in dictionary
                        where entry.Value == " ";
                         `

最佳答案

试试这个:

    public bool Authenticate()
    {
        Console.WriteLine("Please enter a username");
        string inputUsername = Console.ReadLine();
        Console.WriteLine("Please enter your password");
        string inputPassword = Console.ReadLine();
        return dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword;
    }

关于c# - 两个字符串变量与返回 bool 值的私有(private)字典进行比较c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279676/

相关文章:

c# - 如何在不重启服务器的情况下添加带有触发器的作业以运行 Quartz.NET 调度程序实例?

c# - DuoCode 和 Knockout

c# - 用于检查 null 或空的 Linq Lambda 表达式

java - 如何使用 Java 代码加快 Selenium Webdriver 中 iFrame 之间的切换?

c# - Lucene.NET 2.9 自定义过滤器添加授权

c# - 我们在以下代码中使用 "using statement"的原因是什么?

c# - 如何使用 GTKSharp 打印图像

c# - 如何在 ASP.NET 中使用时区?

C# 在线程中使用 Process.Start

c# - 是否有必要在控制处理之前明确清除数据绑定(bind)?