I am trying to validate a logged in user when they are registered by the Administrator, the users are given a LoginID
and password of @password
and a LoginCount
of 0, by default. If the user eventually logs in and updates the password it gets hashed then the loginCount updates to 1. Refer to the image below:
I am trying to do multiple if statements when the user is logging in for the first time, it should check against a string password:
if (loginCount < 1 && password == "@password") {
And if the user has logged in before it should check against a hashed password using the password hashing class:
if(loginCount > 0 && PasswordHasher.Verify(password, hashed))
All this is done after the user has been filtered from the database using the LoginID:
select * from cor_usersetup where LoginID = @LoginID
but it's not working.
public bool GetAuthentication(string loginId, string password) {
var connection = System.Configuration.ConfigurationManager.ConnectionStrings["FCoreDBConnection"].ConnectionString;
SqlConnection cn = new SqlConnection(connection);
string sql = "select * from cor_usersetup where LoginID = @LoginID"; // and Password = @Password
SqlCommand cmd = new SqlCommand(sql,cn);
cmd.Parameters.AddWithValue("@LoginID", loginId);
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
int counter = 0;
while (rdr.Read()) {
var hashed = rdr["Password"].ToString().Trim();
var loginCount = int.Parse(rdr["LoginCount"].ToString());
if (loginCount < 1 && password == "@password") { //if the user hasnt changed his/her password yet......
counter = counter + 1;
}
if(loginCount > 0 && PasswordHasher.Verify(password, hashed)) { //after password confirmation..
counter = counter + 1;
}
}
rdr.Close();
cn.Close();
if (counter != 0) {
success = true;
}
return success;
}
I set breakpoints refer to the image below but its not hitting the if
statements:
What am I getting wrong, and did I put my if condition properly in the right place?