首页 文章

验证数据集的电子邮件和密码输入

提问于
浏览
0

我正在尝试根据数据库中的数据集结果验证表单中的用户输入 . 我不知道我是在实现逻辑还是在foreach循环中尝试(我有点基于逻辑来实现) . 我试图将数据表的索引(从数据集创建)分配给变量,然后检查它们是否与用户输入匹配 . 这是一个体面的方式吗?可能吗?我怎样才能做到这一点?

我从代码中得到一个错误我认为我正确实现了你可以在代码的注释中找到 .

protected void btnSubmit_Click(object sender,EventArgs e){//为数据集创建连接以填充验证字符串的电子邮件和密码ConnectionString = ConfigurationManager.ConnectionStrings [“ContourCoffeeRoastersConnectionString”] . ConnectionString; ; SqlConnection Conn = new SqlConnection(ConnectionString); Conn.Open();

//creats a data set with database information and puts the dataset in a datatable
    SqlDataAdapter daCustomer = new SqlDataAdapter("Select (CustEmail, CustPW) From Customer", Conn);
    DataSet dsEmailsandPW = new DataSet("Emails");
    daCustomer.Fill(dsEmailsandPW, "Customers");//I get and error here in my stack trace
    DataTable tblCustomers;
    tblCustomers = dsEmailsandPW.Tables["Customers"];

    //sets the variable to user inputed data from the login form so it can be compared and validated to the dataset
    string custEmail = exampleInputEmail1.Text;
    string custPW = exampleInputPassword1.Text;

    //looks through each row on the data set to see if a matching email can be found
    foreach (DataRow drCurrent in tblCustomers.Rows)
    {
        string txtEmail = drCurrent[0].ToString();//sets a variable to the first index of the current row of the dataset
        if (txtEmail == custEmail)//if a match is found with the user input and a record in the database through the data set the password is then checked for validation
        {
            string txtPW = drCurrent[1].ToString();//assigns a vaiable to the second index of the row that should contain customer password
            if (txtPW == custPW)//if the password is a match 
            {
                lblLogin.Text = "You are logged in!";
                //TODO: query for cartID and set it to the cookie!!!!! 
            }
            else
            {
                lblLogin.Text = "Email/username combination is not correct";
            }
        }
        else
        {
            lblLogin.Text = "Email/username combination is not correct";
        }
    }

1 回答

  • 0

    我建议发送一个查询

    "SELECT CustEmail FROM Customer where CustPW =" + custPw;
    

    并将其评估为布尔值 . 如果它回来了,你有一个有效的登录 . 如果它返回false,则表示登录无效 .

    你也可以这样做

    "SELECT CustEmail FROM Customer where CustPW =" + sha1(custPw);
    

    如果密码已加密,您可以加密他们尝试的登录密码,并根据数据库的加密密码进行检查 .

    编辑:我还建议将您的查询存储为存储过程,只提供可变数据 .

相关问题