且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对C#代码中的登录有疑问

更新时间:2023-02-08 15:46:43

这是登录页面的逻辑.
它首先检查用户名和密码,它为空,显示消息
"Please make sure that the username and the password is Correct"

如果不为空
调用Validate_login函数,该函数将返回数字,如果返回值为1,则会显示消息
"Login is Good, Send the User to another page or enable controls"
其他显示
"Invalid Login"

您为什么要这么做?
it''s a login page logic.
it checks first for username and password, it it is empty, shows message
"Please make sure that the username and the password is Correct"

if not empty
call Validate_login function which will return number, if return value is 1 shows message
"Login is Good, Send the User to another page or enable controls"
else shows
"Invalid Login"

why do u want that ?


if (txtUsername.Text != "" && txtPassword.Text != "")
{
    Results = Validate_login(txtUsername.Text, txtPassword.Text);
}
if (Results == 1)
{
    lblMessage.Text = "Login is Good, Send the User to another page or enable controls";
    //here should be the actual redirection, not only the message
}
else
{
    lblMessage.Text = "Invalid Login";
    lblMessage.ForeColor = System.Drawing.Color.Red;
    //Dont Give too much information this might tell a hacker what is wrong in the login
}



如果用户名和密码不为空,则这段代码仅检查用户是否对另一个函数Validate_login有效.



This code just check if the user is valid with another function Validate_login provided that username and password are not empty.


这应该很简单.该代码本身告诉您它应该做什么.您应该仔细看看.

但是,这里假设您是初学者:

That should be straightforward. The code itself tells you what it''s supposed to do. You should take a closer look.

However, here you go assuming you are a beginner:

if (txtUsername.Text != ""; txtPassword.Text != "")
   {
       Results = Validate_login(txtUsername.Text, txtPassword.Text);
   }
   else
   {
       lblMessage.Text = "Please make sure that the username and the password is Correct";
   }



此代码块从两个文本框txtUserName和txtPassword获取用户输入.使用.Text属性,首先检查来自两者的字符串以查看是否输入了值.一旦验证通过,便调用Validate_login方法进行检查(是的,是的),以验证输入的用户名和密码.返回值存储在变量Results中.

在第二种情况下,如果用户名或密码完全为空,则标签会显示一条消息.

现在是第二个.



This code block gets the user input from the two textboxes txtUserName and txtPassword. Using the .Text property the string from both is checked first to see that a value is entered. Once that is verified then a method Validate_login is called to check, yep you guessed it, to validate the username and the password entered. The return value is stored in the variable Results.

In the second case where either username or password is totally blank, a label is given a message to be displayed.

Now the second one.

if (Results == 1)
   {
       lblMessage.Text = "Login is Good, Send the User to another page or enable controls";
   }
   else
   {
       lblMessage.Text = "Invalid Login";
       lblMessage.ForeColor = System.Drawing.Color.Red;
       //Dont Give too much information this might tell a hacker what is wrong in the login
   }



使用保存返回值的变量,检查变量是否包含值"1"或其他值.



Using the variable which holds the return value the variable is checked to see if it holds the value "1" or something else.