-2

Quick question - is it possible to change the password of a user that has been entered into the "user" table under the MySQL database via a WinForm app?

I've been inserting and selecting data from a database I created, but if I ever wanted to change the MySQL password, I was wondering if this is possible via C#. Reason I say this is because ATM the connectionstring is hard-coded to take the username and password (maybe someone can give advice on how not to store the password in the connection string in App.config too).

Thanks :)

sushi7777
  • 179
  • 3
  • 16
  • You can access the sys database. it stores the users and their passwords. – Hagai Wild Jul 23 '18 at 21:39
  • i would not store or hardcore a MySQL username/password with in a C# application at all. Especially if the C# application is meant to be downloadable and usable for the public.. Because a (C#) disassembler would make it possible to find the user name and password to a MySQL server... If the application is meant for internal use like a trusted network off people i think you will be ok. – Raymond Nijland Jul 23 '18 at 21:45
  • @HagaiWild can't see where the users and passwords are stored in that database. Plus why are people downvoting this question? – sushi7777 Jul 23 '18 at 21:46
  • @RaymondNijland thanks Raymond. Any suggestions on where else the connection string can be stored in order to connect to the MySQL database? Also, in regards to changing the password for a user stored in the "user" table, is it possible to modify the password through the app? I think the reasoning behind this was so that you don't have to rebuild the app everytime someone changes the MySQL password. – sushi7777 Jul 23 '18 at 21:48

1 Answers1

0

Take something like this post and change the statement in the command string to use an ALTER USER instead:

using(MySqlConnection Connection = new MySqlConnection("SERVER=localhost;UID=root;"))
using(MySqlCommand Command = new MySqlCommand("ALTER USER 'username'@'localhost' IDENTIFIED BY 'password';", Connection))
{
    Connection.Open();
    Command.ExecuteNonQuery();
}
Andrew Harris
  • 1,419
  • 2
  • 17
  • 29
  • Thanks Andrew. ATM my connection string points to another database hosted by my server So will I have to add another connection string but instead have the database as "mysql"? – sushi7777 Jul 23 '18 at 21:52
  • 1
    Not sure I understand your issue. My answer will help you change the MySQL Users password. If you are changing the password of the user that is specified/hard coded in the Connection string then you will need to look at something like in this post to update the password https://stackoverflow.com/questions/360024/how-do-i-set-a-connection-string-config-programmatically-in-net – Andrew Harris Jul 23 '18 at 22:01
  • Perfect - thanks man. Also, if your MySQL database is older than 5.7.5 (which mine is), the above does not work. However, doing something like `SET PASSWORD FOR 'user-name-here'@'hostname' = PASSWORD('new-password');` does :). – sushi7777 Jul 23 '18 at 22:05