c# - How to hardcode login for username and password? -
i have windowadmin table. hardcode 4th record in table. if user key in username = admin , password = 123, log him in. below images , codes. thank much.

private void btnadminlogin_click(object sender, eventargs e) { //retrieve connection information info app.config string strconnectionstring = configurationmanager.connectionstrings["sacpconnection"].connectionstring; //step 1: create connection sqlconnection myconnect = new sqlconnection(strconnectionstring); //step 2: create command //for windowsadmin string strcommandtext = "select * windowsadmin"; strcommandtext += " winusername=@aname , winpassword=@apwd;"; sqlcommand cmd = new sqlcommand(strcommandtext, myconnect); //for windowsadmin cmd.parameters.addwithvalue("@aname", txtadminusername.text); cmd.parameters.addwithvalue("@apwd", txtadminpasswd.text); try { // step 3: open connection , retrieve data calling executereader myconnect.open(); // step 4: access data sqldatareader reader = cmd.executereader(); while (reader.read()) //for windowsadmin { if (messagebox.show("login successful") == dialogresult.ok) { admin form = new admin(txtadminusername.text); form.show(); /* login loginform = new login(); this.visible = false; this.hide(); adminlogin adminloginform = new adminlogin(); this.visible = false; this.hide(); */ return; } } //step 5: close connection reader.close(); messagebox.show("invalid username or password "); } }
i think should code
private void btnadminlogin_click(object sender, eventargs e) { if (txtadminusername.text=="<hardcoded_username>" && txtadminpasswd.text=="<hardcoded_password>") { //login hardcoded user messagebox.show("login successful") == dialogresult.ok //do stuff return; } //retrieve connection information info app.config string strconnectionstring = configurationmanager.connectionstrings["sacpconnection"].connectionstring; //step 1: create connection sqlconnection myconnect = new sqlconnection(strconnectionstring); //step 2: create command //for windowsadmin string strcommandtext = "select * windowsadmin"; strcommandtext += " winusername=@aname , winpassword=@apwd;"; sqlcommand cmd = new sqlcommand(strcommandtext, myconnect); //for windowsadmin cmd.parameters.addwithvalue("@aname", txtadminusername.text); cmd.parameters.addwithvalue("@apwd", txtadminpasswd.text); try { // step 3: open connection , retrieve data calling executereader myconnect.open(); // step 4: access data sqldatareader reader = cmd.executereader(); while (reader.read()) //for windowsadmin { if (messagebox.show("login successful") == dialogresult.ok) { admin form = new admin(txtadminusername.text); form.show(); /* login loginform = new login(); this.visible = false; this.hide(); adminlogin adminloginform = new adminlogin(); this.visible = false; this.hide(); */ return; } } //step 5: close connection reader.close(); messagebox.show("invalid username or password "); } }
Comments
Post a Comment