如何在C#中验证用户名和密码?

如何在C#中验证用户名和密码?,c#,C#,我试图编写一些简单的c代码来验证用户名和密码,用户名和密码都已经在代码中了。此外,验证来自一个简单的用户名和密码,而不是来自任何sql数据库 如果验证正确,我希望打印(输出)-“正确标识”,如果用户名和/或密码错误,我希望输出-“错误标识” 我的问题(由crashmstr发布)如何检查用户是否输入了硬编码的用户名和密码。这导致-OP似乎不知道如何检查 为什么我的代码输出“正确标识”,而不管输入是什么 string username = "Pinocchio";

我试图编写一些简单的c代码来验证用户名和密码,用户名和密码都已经在代码中了。此外,验证来自一个简单的用户名和密码,而不是来自任何sql数据库

如果验证正确,我希望打印(输出)-“正确标识”,如果用户名和/或密码错误,我希望输出-“错误标识”

我的问题(由crashmstr发布)如何检查用户是否输入了硬编码的用户名和密码。这导致-OP似乎不知道如何检查

为什么我的代码输出“正确标识”,而不管输入是什么

            string username = "Pinocchio";
            string password = "Disney";

            Console.WriteLine("Enter Username: ");
            char answer = console.ReadLine()[0];

            Console.WriteLine("Enter Password: ");
            char answer2 = console.ReadLine()[1];

            if (!(username == "Pinocchio" && password == "Disney")) {
                Console.WriteLine("Correct Identification");

            }

            else
            {

                 Console.WriteLine("Wrong Identification");
            }
        }
    }
}
我有一个有效的。。。我可以在以后添加更多的代码

string password = "hello";
string username = "how";

if(Console.ReadLine() == password && Console.ReadLine() == username)
{
    Console.WriteLine("Correct Identification");
}
else 
{
    Console.WriteLine("Wrong Identification");
}

让我们把你的表情分解一下

这:

由于两个字符串都匹配,因此产生
true

然后你放置一个
在它前面:

(!(username == "Pinocchio" && password == "Disney"))
这就是
!true
,即
false
。因此,用户名和密码被认为是错误的

刚刚删除了

(username == "Pinocchio" && password == "Disney")

我想你需要这样的东西:

Console.WriteLine("Enter User name: ");
string enteredUsername = console.ReadLine();

Console.WriteLine("Enter Password: ");
string enteredPassword = console.ReadLine();

if (username == enteredUsername && password == enteredPassword)
{ ... }

那么你的问题到底是什么?还有一个提示:不要将密码以明文和硬编码的形式存储在您的
if
中,如果您不使用
answer
answer2
,则密码可能重复,当然,你会得到真正的路径,因为你自己设置了这些值。你的问题是什么?我很好奇为什么你要接受一个
char
作为用户名和密码。除了用户输入是
answer
answer2
…没有代码对输入执行任何检查。输入被忽略。是的,但输入变量未被使用,用户名/密码变量设置为硬编码。我认为问题在于如何检查用户是否输入了硬编码的用户名和密码,而OP似乎不知道如何检查。(换句话说:是的,OP的代码忽略了用户输入,但它可能应该对其进行处理)。
Console.WriteLine("Enter User name: ");
string enteredUsername = console.ReadLine();

Console.WriteLine("Enter Password: ");
string enteredPassword = console.ReadLine();

if (username == enteredUsername && password == enteredPassword)
{ ... }