mysql - (C#) Reading Username and password from SQL Database for a login -
i've taken interest in hosting own database application's login of have provider allows me use panel have hooked mysql database. made little test login see if figure out how it. code , haven't been able work keep getting login failed i've tried couple different ways appreciated. , know i'm connected database. edited server ip , password privacy purposes. database: http://prntscr.com/ettqn1
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using metroframework.forms; using metroframework.design; using metroframework; using mysql.data.mysqlclient; namespace mysql_testing { public partial class form1 : metroform { static mysqlconnection sqlconnection = new mysqlconnection("server=myserveraddress; database=logins; uid=root; pwd=mypass;"); static string sqlquerry = "select usernames accounts;"; static string sqlquerrypass = "select password accounts;"; static string sqlquerryid = "select id accounts;"; static mysqlcommand cmd = new mysqlcommand(sqlquerry, sqlconnection); static mysqlcommand cmd2 = new mysqlcommand(sqlquerrypass, sqlconnection); static mysqlcommand cmd3 = new mysqlcommand(sqlquerryid, sqlconnection); public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } private void metrobutton1_click(object sender, eventargs e) { sqlconnection.open(); //mysqldatareader rdid = cmd3.executereader(); //rdid.close(); mysqldatareader rd = cmd.executereader(); list<string> readuser = new list<string>(); readuser.add(convert.tostring(rd.read())); rd.close(); mysqldatareader rdpass = cmd2.executereader(); list<string> readpass = new list<string>(); readpass.add(convert.tostring(rdpass.read())); if (readuser.toarray().tostring().contains(metrotextbox1.text) && readpass.toarray().tostring().contains(metrotextbox2.text)) { messagebox.show("login successful"); } else { messagebox.show("login failed"); } rdpass.close(); sqlconnection.close(); } } }
first, should store password field hash random salt, instead of plain text.
you don't need table
accounts3 queries, , not have relation. how know user - password corresponding 3 different queries.
the condition
readuser.toarray().tostring().contains(metrotextbox1.text) && readpass.toarray().tostring().contains(metrotextbox2.text) it means accounts table has username , password matched perhaps doesn't belong 1 person.
you use 1 query check login information:
select * accounts username = @username , password = @password
with parameter passed textbox.
reference link: store password hash
Comments
Post a Comment