C# can';“t显示”;错误的pw“;

C# can';“t显示”;错误的pw“;,c#,asp.net,C#,Asp.net,我在下面有一个简单的登录页面 如果我输入正确的ID+pw->success(我想要的) 如果我输入了错误的ID->错误的登录(我想要) 但是如果我输入了正确的ID+错误的ID,我希望它显示错误的密码 我怎么做 多谢各位 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

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

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

如果我输入了错误的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");
    }
}

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

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");
    }
}

虽然这不能回答您的问题,但我发现您的逻辑存在一个重大的安全缺陷。我认为无论您的用户遇到什么故障,无效用户名或无效密码,您都应该始终显示相同的“无效登录”消息

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


只是想一想。

从数据库中读取firstname和lastname,然后对照lastname检查密码。我怀疑此字段是否包含有效密码

对于此逻辑错误的一部分,您应该在语句中使用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";
              }
         }
     }
 }

要记住的另一点是以下几点。在数据库中,您不应该有明文密码。存储密码的正确方法是存储与密码对应的散列字符串,然后对用户输入应用散列函数,并检查数据库中是否存在相同的散列字符串

Employees表中记录了多少用户?如果您有多个用户注册,则此代码存在严重问题。您不应显示任何消息,如“错误的密码”或“错误的用户名”。始终给出一条含糊不清的错误消息,说明用户名或密码错误。否则,您将提示攻击者用户名是正确的,您永远不应该这样做。@LasseV.Karlsen谢谢。谢谢您的建议。+1的安全逻辑。还有一件事。您无法判断u/p是否不正确,因为可能有多个用户使用相同的密码。