If statement running both conditions, even though its IMPOSSIBLE to run the second condition if the first is met c# -
i have code, logging in software i'm writing, code checks username , password against file containing username , hashed passwords, however, if input other login stored on first line in file, both parts of if statement run, though impossible 'else' run if first condition met.
i can't seem find issues code, , of course, if login details correct, logs user in, though gives error saying cannot log in.
code:
system.io.streamreader file = new system.io.streamreader("userdata.txt"); string fileline; string line; while ((line = file.readline()) !=null) { fileline = line; system.diagnostics.debug.writeline(fileline); string len = fileline.length.tostring(); string usr = fileline.substring(0, 20); string hash = fileline.substring(20, 32); if (usr.contains(usernameipt.text) && hash == password) { globalvar.usernameentered = usernameipt.text; showuserpanel(); break; } else { messagebox.show("error, password or username incorrect. \r\n\r\nyou may not have account set use software, please contact system administartor assistance.", "login error"); } }
let me explain code in pseudo code
lets pretend file contains names (to make life shorter) lets has
jon kate alison fred bill
so code says give me name, type in pete
your code opens file , reads jon , goes "error"
reads kate goes "error"
reads alison goes "error"
...
so
you need change code slightly
pick flag call "found" , set false
if pick kate time,
reads jon - nothing
reads kate - sets found , breaks
if found ok - else "error"
if picked pete end of file, found false, , once "error"
Comments
Post a Comment