将两个字符串变量与返回布尔值的私有字典进行比较c#
本文关键字:字典 比较 布尔值 返回 两个 变量 字符串 | 更新日期: 2023-09-27 18:18:45
我试图创建两个名为username和password的字符串变量,并返回一个名为authenticated的布尔值。我正试图将用户名和密码字符串与私有字典的内容进行比较。如果用户名和密码匹配,我希望布尔值设置为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;
}