【问题标题】:Validating Email and Password Input Against a Dataset根据数据集验证电子邮件和密码输入
【发布时间】:2023-03-21 11:15:01
【问题描述】:

我正在尝试根据我的数据库中的数据集的结果来验证来自表单的用户输入。我不知道我是在实现逻辑还是在尝试 foreach 循环(我只是根据逻辑编造的)。我正在尝试将数据表的索引(从数据集创建)分配给变量,然后检查它们是否与用户输入匹配。这是一个体面的方法吗?可能吗?我怎样才能使这项工作?

我从代码中得到一个错误,我认为我正确地实现了你可以在代码的 cmets 中找到的代码。

protected void btnSubmit_Click(object sender, EventArgs e) { //为数据集创建连接以填充电子邮件和密码以进行验证 字符串 ConnectionString = ConfigurationManager.ConnectionStrings["ContourCoffeeRoastersConnectionString"].ConnectionString; ; SqlConnection 连接 = 新的 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";
        }
    }

【问题讨论】:

    标签: c# asp.net validation datatable dataset


    【解决方案1】:

    我建议发送查询

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

    并将其评估为布尔值。 如果它返回 true,则您有一个有效的登录名。 如果返回 false,则您的登录无效。

    你也可以这样做

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

    如果密码被加密,您可以加密他们尝试的登录密码并检查它 针对数据库的加密密码。

    编辑:我还建议将您的查询存储为存储过程并仅提供可变数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 2020-06-17
      • 2015-11-14
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2015-08-22
      相关资源
      最近更新 更多