I'm trying to use this again, but am coming up against the same problem.
I have 2 db fields, hash and salt, both VARCHAR (255) fields.
When I add a user, I can see the detail in the db table, but when I try logging in using the password, it says the details don't match.
To create the user, I user:-
Code:
public static bool AddUser(int ID, User user)
{
bool result = false;
string salt = Global.CreateSalt(12);
string passhash = Global.CreatePasswordHash(user.password, salt);
MySqlConnection conn = new MySqlConnection(Global.ConnStr);
MySqlCommand comm = new MySqlCommand("spAddUser", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("username", user.username);
comm.Parameters.AddWithValue("forename", user.forename);
comm.Parameters.AddWithValue("surname", user.surname);
comm.Parameters.AddWithValue("email", user.email);
comm.Parameters.AddWithValue("pass", passhash);
comm.Parameters.AddWithValue("salt", salt);
comm.Parameters.AddWithValue("admin", user.admin);
comm.Parameters.AddWithValue("userId", ID);
MySqlParameter id = new MySqlParameter("ref", MySqlDbType.Int32);
id.Direction = ParameterDirection.Output;
comm.Parameters.Add(id);
using (conn)
{
using (comm)
{
try
{
conn.Open();
comm.ExecuteNonQuery();
user.id = (int)id.Value;
if (!UserRoleDAL.addUserRoles(user)||!UserRoleDAL.adduserCurrency(user))
{
result = false;
}
else
{
result = true;
}
}
catch (MySqlException ex)
{
user.err = ex.ToString();
}
}
}
return result;
}
And to login, I use
Code:
public static int Login(string username, string password)
{
int result = 0;
MySqlConnection conn = new MySqlConnection(Global.ConnStr);
MySqlCommand comm = new MySqlCommand("spLogin", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("username", username);
comm.Parameters.AddWithValue("ipAddress",HttpContext.Current.Request.UserHostAddress);
bool timeOut = false;
if(HttpContext.Current.Request.Cookies["myCook"]!=null) timeOut=true;
comm.Parameters.AddWithValue("timedOut",timeOut);
MySqlDataReader rs;
using(conn)
{
using(comm)
{
try{
conn.Open();
rs = comm.ExecuteReader();
if(rs.HasRows)
{
rs.Read();
if(Global.CreatePasswordHash(password,(string)rs["PasswordSalt"])==rs["PasswordHash"])
result = (int)rs["ID"];
HttpContext.Current.Request.Cookies.Set(new HttpCookie("myCook", "1"));
}
rs.Close();
}
catch(MySqlException ex)
{
HttpContext.Current.Response.Write(ex.ToString());
}
}
}
return result;
}
}
The add user returns a value fine and I can see the user in the table, so I know it's been added. Not sure why the login won't work though.
I also checked my SP in Query browser and that is returning a record.
Any ideas?
Bookmarks