无法显示“错误的PW”

本文关键字:错误的 PW 错误 显示 | 更新日期: 2023-09-27 18:34:06

我在下面有这个简单的登录页面,

如果我输入正确的ID + pw ->成功(我想要)

如果我输入错误的ID ->错误的登录(我想要)

但是如果我输入正确的ID+错误的ID,我希望它说错误的密码。

我该怎么做?

谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["X"] != null)
        {
            Response.Redirect("MemberPage.aspx");
        }
    }
    SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
    protected void Button1_Click(object sender, EventArgs e)
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                if (TextBox1.Text == dr.GetString(0) || TextBox2.Text == dr.GetString(1))
                    {
                            Session["x"] = TextBox1.Text;
                            Response.Redirect("MemberPage.aspx");
                    }
                else
                {
                    Label2.Text = "wrong login";
                }
            }
        }
        cnn.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}

无法显示“错误的PW”

虽然

这不能回答您的问题,但我看到您的逻辑存在重大安全漏洞。我认为无论您的用户遇到什么故障,用户名无效或密码无效,您都应该始终显示相同的"无效登录"消息。

如果您有人试图闯入系统,一旦您验证用户帐户存在(密码无效),他们就可以开始使用蛮力破解该特定帐户的密码。

只是想一想。

你把

你的逻辑放错了。 逻辑将是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["X"] != null)
        {
            Response.Redirect("MemberPage.aspx");
        }
    }
    SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
    protected void Button1_Click(object sender, EventArgs e)
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
                if (TextBox1.Text.Trim() == dr.GetString(0) || TextBox2.Text.Trim()== dr.GetString(1))
                    {
                        if (TextBox2.Text.Trim()== dr.GetString(1))
                        {
                            Session["x"] = TextBox1.Text.Trim();
                            Response.Redirect("MemberPage.aspx");
                        }
                        else
                        {
                            Label2.Text = "wrong password";
                        }
                    }
                else
                {
                    Label2.Text = "wrong login";
                }
        }
        cnn.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}

您从数据库中读取名字和姓氏,然后根据姓氏检查密码。我怀疑此字段是否包含有效的密码

作为此逻辑错误的一部分,您应该在语句中使用 WHERE 子句来检查用户是否存在于数据库中。

protected void Button1_Click(object sender, EventArgs e)
{
    // Command with parameters that check if a user with the supplied credentials exists
    // If the user exists then just one record is returned from the datatable....
    string cmdText = "SELECT FirstName,LastName " + 
                     "FROM Employees " + 
                     "WHERE username=@uname and pass=@pwd";
    using(SqlConnection cnn = new SqlConnection(.....))
    using(SqlCommand cmd = new SqlCommand(cmdText, cnn))
    {
         cnn.Open();
         cmd.Parameters.AddWithValue("@uname", TextBox1.Text);
         cmd.Parameters.AddWithValue("@pwd", TextBox2.Text);
         using(SqlDataReader reader = cmd.ExecuteReader())
         {
              // If the Read returns true then a user with the supplied credentials exists 
              // Only one record is returned, not the whole table and you don't need to 
              // compare every record against the text in the input boxes 
              if(reader.Read())
              {
                   Session["x"] = reader.GetString(0);
                   Response.Redirect("MemberPage.aspx");
              }
              else
              {
                   Label2.Text = "Invalid credentials";
              }
         }
     }
 }

要记住的另一点如下。在数据库中,不应使用明文密码。存储密码的正确方法是存储与密码对应的哈希字符串,然后将哈希函数应用于用户输入并检查数据库中的相同哈希字符串