登录屏幕显示不正确的凭据

时间:2014-03-05 10:40:19

标签: c#

我正在c#中创建一个登录屏幕。我如何制作它以便具体显示用户名或密码是否不正确。

private void btnSubmit_Click(object sender, EventArgs e)
{
    {
        string username = "Tim";
        string password = "Hennings";
        if ((this.txtUsername.Text == username) && (this.txtPassword.Text == password)) 
        {
            if (txtUsername.Text == username && txtPassword.Text == password)
                MessageBox.Show("Log in successful");
        }
        else
        {
            MessageBox.Show("Wrong Username/Password please try again");
            txtUsername.Focus();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

  

我如何制作它以便具体显示用户名或密码是否不正确[?]

你不应该。当您向攻击者提供确切错误的详细信息时,这是一个安全问题。

有关详细信息,请参阅:

“Username and/or Password Invalid” - Why do websites show this kind of message instead of informing the user which one was wrong?

答案 1 :(得分:0)

        string username = "Tim";
        string password = "Hennings";
        if(this.txtUsername.Text != username)
        {
            MessageBox.Show("Wrong Username please try again");
            txtUsername.Focus();
        }else if(this.txtPassword.Text != password)
        {
            MessageBox.Show("Wrong Password please try again");
            txtPassword.Focus();
        }else
        {
            MessageBox.Show("Log in successful");
        }

答案 2 :(得分:0)

private void btnSubmit_Click(object sender, EventArgs e)
    {
         {
            string username = "Tim";
            string password = "Hennings";
            string outputMessage = string.Empty;

            if (this.txtUsername.Text != username)
            {
                outputMessage = "Username incorrect";
            }

            if (this.txtPassword.Text != password)
            {
                outputMessage = "Password incorrect";
            }

            if (!string.IsNullOrEmpty(outputMessage)) 
            {
                MessageBox.Show(outputMessage);
            }
            else
            {
                // Password and Username matched so log them in.
            }

        }
    }