Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am having difficulty in changing the password to my winform in C#.
I already have the login page successfully opens the second form.
I also have a change password button that opens another dialog with three textboxes, one for the old password, one for the new password and the third textbox is to confirm the new password. Do I use the Update, SET directing to the password column? Or can I still use the UPDATE SET for both?

C#
query = "UPDATE Users SET UserName = 'UserName'" + "Password = 'Password'";


So this will replace the original user name and password?

Thanks.
Posted
Updated 1-Sep-11 1:37am
v2

In addition to what Prerak said, you can do
SQL
query = "UPDATE Users SET Password='" + Password + "' where UserName = '" + UserName+ "' AND Password='" + OldPassword + "'";

In this way, no one can change the password without the old password matching as well.
 
Share this answer
 
v2
you should implement functionality to check user Entering Old password is right or wrong and also having functionality to validate new password and re-enter new password is matching or not. if all value went true then execute "Update" command.

if(txtnewpassword.Text == txtrenewpassword.Text)
{
query = "UPDATE Users SET Password = '"+txtnewpassword.Text+"' where 
UserName ='UserName' and Password='"+txtoldpassword.Text+"'";
// Execute query using ExecuteNonQuery function. 
}


you can also take a look at given question having some nice solution.
validating and changing a user's password[^]
 
Share this answer
 
v2
Comments
version_2.0 1-Sep-11 8:11am    
my 5..
It should be something like this
C#
query = "UPDATE Users SET Password='" + Password + "' where UserName = '" + UserName+ "'";
 
Share this answer
 
Comments
Abhinav S 1-Sep-11 8:09am    
My 5. You provided the basic answer.
The basic solution has already been given. In addition to that simple query, some things you should always remember when dealing with passwords and database queries:

  • The password should be hashed in the database. This means that if someone gets hold of your database they don't have all your users' passwords – particularly important as many people re-use passwords for several services. This means that you should be hashing the password string before setting it in the UPDATE statement, and if you want to follow Abhinav's/Ravi's advice and check the old password too, you should hash the old password text box's contents before putting it in the WHERE clause, too.
  • Use parameterised queries if you can. If you can't, at least make sure that you always escape user text which is to be entered into a query. Queries built up from text are how SQL injection vulnerabilities get into software, and it should be a reflex when you build a query to either use parameters or to escape everything you are putting into that string from textual user input.


So the pseudocode for your process should be something like
bool UpdatePassword(string username, string old, string new1, string new2){
 if(new1 != new2) return false;
 
 Query q = new Query("update users set password='@NewPassword' where username='@Username' and password='@OldPassword'");
 q.SetParameter("NewPassword", SHA1(username + new1));
 q.SetParameter("OldPassword", SHA1(username + old));
 q.SetParameter("Username", SHA1(username));
 return 1 == q.Execute().AffectedRows;
}


How exactly you set up a parameterised query may depend on the interface you're using to talk to the database, but hopefully it is similar enough to that pseudocode that you can adapt it. Replace SHA1 with your hash of choice and add salts etc to the hashing process if you like.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900