validating and changing a user's password

雨燕双飞 提交于 2019-12-05 18:01:12

You really should not be storing these passwords in plain text. You should hash the password and store the hash. Then if you want to check if a password is correct hash the password the user typed and compare it to the hash stored for the user.

But, it sounds like you need help getting a value out of the database for the current user. Putting something like this in there, ought to do this for you. Please note that like I said above, this should really be retrieving a hash of the password, not the actual password in plain text.

string sqlquery = "SELECT Password FROM [Member] where Username=@username";
SqlCommand cmd = new SqlCommand(sqlquery, connect);
cmd.Parameters.AddWithValue("@username", label_username.Text);
cmd.Connection = connect; 
string currentPassword = (string)cmd.ExecuteScalar();

if (currentPassword == textBox_Current.Text)
{
 // PASSWORD IS CORRECT, CHANGE IT, NOW.
} else {
 // WOW EASY BUDDY, NOT SO FAST
}

First you should use password hashing in your application, thus the password fields of the database should hold the hashed values.

Assuming this, to accomplish your goals,

  1. consider your string username -> Hash it -> write a query to check whether that hashed value and the user's password's hash value stored in the database is the same
  2. consider string password and string newPassword in your code -> Hash both -> check whether the hash values are the same
  3. consider string password and string newPassword -> check whether each is null or the length is 0

Also you should perform these tasks in the following order:

1 -> 3 -> 2

Hope this helps...

   protected void btn_PasswordChange(object sender, EventArgs e)
    {
        string constring = DataAccess.GetConnection();
        SqlConnection con = new `SqlConnection`(constring);

        {
            if (con.State != ConnectionState.Open)
                con.Open();
        }
        string str = "select * from tbl_MemberLogin where Password='" + txtoldpwd.Text + "'";
        DataTable DT = new DataTable();
        DT = objdut.GetDataTable(str);
        if (DT.Rows.Count == 0)
        {
            lblmsg.Text = "Invalid current password";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update tbl_MemberLogin set Password='" + txtnewpwd.Text + "' where UserName='" + Session["UserName"].ToString() + "'";
            cmd.ExecuteNonQuery();
            lblmsg.Text = "Password changed successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!