系统无法识别我的用户名未激活。为什么? [英] My User Name Is Not Recognized by The System is not firing. Why?

查看:128
本文介绍了系统无法识别我的用户名未激活。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单设置,用户可以在其中输入用户名并创建登录密码。当用户输入无效的用户名和密码时,错误消息应该触发但不会触发。它以前工作,我没有改变任何东西。我的问题是什么?



I have a form setup to where a user can enter their username and create a password to login. When a user enters an invalid username and password the error message should fire but it doesn't. It was working before and I didn't change anything. What is my issue?

protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into Tablepass (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        string insCmd2 = "Insert into Tablepass (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";

        SqlCommand insertUser = new SqlCommand(insCmd, con);
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);

        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Is Not Recognized by The System!!!');", true);
            
        }
        finally
        {
        }
    }

    protected void TextBoxEA_TextChanged(object sender, EventArgs e)
    {

        using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString))
        {
            con.Open();

            SqlCommand scmd = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from Table1 where EmailAddress = @TextBoxEA", con);
            SqlCommand scmd2 = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from Table2 where EmailAddress = @TextBoxEA", con);

            scmd.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));
            scmd2.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));

            TextBoxINST_ID.Text = string.Empty;
            TextBoxaccessLevel.Text = string.Empty;

            using (SqlDataReader dr = scmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    TextBoxINST_ID.Text = dr["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr["accessLevel"].ToString();
                    
                }
            }

            using (SqlDataReader dr2 = scmd2.ExecuteReader())
            {
                while (dr2.Read())
                {
                    TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr2["accessLevel"].ToString();
                    
                }
            }

            }
        }
    }

推荐答案

请参阅上述问题的评论。



将密码存储在数据库中是不明智的。 请参阅此文章以获得解释。 [ ^ ]

下面是实现目标的简单方法,它只是逻辑的模型而不是理想的方法。



See comments to question above.

It is not wise to store your passwords in a database. See this article for an explanation.[^]
Below is a simple way to achieve your goal and is just a model for the logic and not the ideal way to do this.

protected void Submit_Click(object sender, EventArgs e)
    {
       try
        {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        
 
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress=@EmailAddress and Password=@Password";
       
SqlCommand CheckUser = new SqlCommand(cmdStr, con);
 
        CheckUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        CheckUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);

        con.Open();
        SqlDataReader reader = CheckUser.ExecuteReader();

        DataTable dt1 = new DataTable();
        dt1.Load(reader);
        if (dt1.Rows.Count >= 1)
        {
            Response.Redirect("Login.aspx");      
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert  (''User Name Is Not Recognized by The System!!!');", true);
        }
        catch (Exception ex)
        {
            //do whatever
            
        }
        finally
        {
            con.Close();
        }
    }


这篇关于系统无法识别我的用户名未激活。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆