Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户登录系统中的密码问题。C_C#_Database_Visual Studio 2010 - Fatal编程技术网

C# 用户登录系统中的密码问题。C

C# 用户登录系统中的密码问题。C,c#,database,visual-studio-2010,C#,Database,Visual Studio 2010,我已经在VisualStudio2010中开发了一个web应用程序,正在尝试向该应用程序添加一个用户登录系统。到目前为止,我在验证用户密码时遇到了问题,因为程序会通知我,所有密码都不正确,即使它与该用户名相关联的密码也不正确。这是我到目前为止写的代码: protected void n_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationMan

我已经在VisualStudio2010中开发了一个web应用程序,正在尝试向该应用程序添加一个用户登录系统。到目前为止,我在验证用户密码时遇到了问题,因为程序会通知我,所有密码都不正确,即使它与该用户名相关联的密码也不正确。这是我到目前为止写的代码:

    protected void n_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "select count(*) from Registration where UserName='" + TextBoxUserName.Text + "'";
        SqlCommand CheckUser = new SqlCommand(cmdStr, con);
        int Temp = Convert.ToInt32(CheckUser.ExecuteScalar().ToString());
        if (Temp == 1)
        {
            string cmdStr2 = "Select Password from Registration where UserName ='" + TextBoxUserName.Text + "'";
            SqlCommand pass = new SqlCommand(cmdStr2, con);
            string password = pass.ExecuteScalar().ToString();
            con.Close();

            if (password == TextBoxPassword.Text)
            {
                Session["New"] = TextBoxUserName.Text;
                Response.Redirect("HomePage.aspx");
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Password is invalid";
            }
        }
        else
        {
             Label1.Visible = true;
             Label1.Text = "Username is invalid";
        }


    }
}
}

无论输入什么密码,程序都会输出“password is invalid”(密码无效),这表示问题出在if语句的第一部分?或者它使用的变量? 同样值得一提的是,一个无效的用户名也会以同样的方式标记出来,这很好


提前感谢:

改用。

你正在做的事情对于它应该做的事情来说似乎太复杂了

编辑:我在用户评论后编辑了我的答案:

 public bool Login(String uName, String pasw)
{
    using (SqlConnection myConnection = new SqlConnection(connString))
    {
        string oString = "Select ID from yourTable where username = @username AND paswoord = @password";
        SqlCommand oCmd = new SqlCommand(oString, myConnection);
        oCmd.Parameters.AddWithValue("@username", uName);
        oCmd.Parameters.AddWithValue("@password", pasw);
        string id = null;
        myConnection.Open();
        using (SqlDataReader oReader = oCmd.ExecuteReader())
        {              
            while (oReader.Read())
            {
                id = oReader["id"].ToString();
            }
            myConnection.Close();
        }
        if (id == null)
        {
            return false;
        }
        else
        {
            return true;
        }         
    }
}

你可以试试这样的。此外,它可能与此无关,但某些数据库不喜欢在您命名属性密码时使用它,您可以尝试将其更改为pw或pasw或其他任何形式。

检查SQL注入-您有一个。您的密码应该在数据库中进行加密和散列;你不应该以明文形式存储密码。到目前为止,你的密码看起来非常不安全。谷歌SQL注入攻击,看看我的意思。看看。真的,在数据库中存储密码明文吗?这是什么网站,所以我可以避免使用它…谢谢你的回复!您能否更具体地描述一下整个代码的样子,我对//更多的sql内容等有点困惑。。对不起,我的无礼!我编辑了我的答案。如果您这样做,您所要做的就是使用所需的参数调用login方法。如下所示:bool validInfo=loginTextBoxUserName.Text,TextBoxPassword.Text;然后,您可以检查信息是否有效。当然,这是一个错误,您必须使用自己的连接字符串。。SqlConnection connString=新建SqlConnectionConfiguration Manager.ConnectionString[ConnectionString].ConnectionString;使用SqlConnection myConnection=new SqlConnectionconnStringString connString=ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString将此行粘贴到上面;试试这个。将它放在使用sqlConnection。。。在数据库中是否有名为ID的列?只需将其更改为数据库中存在的列名。如果我看一下您提供的代码,您可以将其更改为密码。所以id=oReader[password],在select子句中只需从表中选择password即可。。