I have an application in vb.net using mysql as database. The application has a login form. There is also a registration form that can enter a new password in the archive using bcrypt.net, as shown at this site:
This is my code for registering a new user (public procedure Clean
is for clean the forms):
Private Sub btSignUp_Click(sender As Object, e As EventArgs) Handles btSignUp.Click
dim hash as string
hash = HashPassword(txConfirmPass.Text)
If (txPass.Text <> txConfirmPass.Text) Then
MessageBox.Show("Passwords don't matches")
Else
Dim sql As String = "INSERT INTO fusion_login(name,user,password) VALUES (@name,@user,@pass)"
Using myconnection As MySqlConnection = Connection.getInstance.getConnection()
Using mycommand As New MySqlCommand()
With mycommand
.CommandText = sql
.CommandType = CommandType.Text
.Connection = myconnection
.Parameters.Add("@name", MySqlDbType.VarChar).Value = txName.Text
.Parameters.Add("@user", MySqlDbType.VarChar).Value = txUser.Text
.Parameters.Add("@pass", MySqlDbType.VarChar).Value = hash
End With
Try
myconnection.Open()
mycommand.ExecuteNonQuery()
If (MessageBox.Show("Do you insert a new user again?", "Register", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
Clean(Me)
Else
Me.Hide()
Login.Show()
End If
Catch ex As MySqlException
MessageBox.Show("Error: " + ex.ToString)
Finally
myconnection.Close()
End Try
End Using
End Using
End If
End Sub
This code works!
Now I'm trying to implement the code that authenticates a user created using the above procedure, but I can't get it to work. Here is what I have so far:
Private Sub btLogin_Click(sender As Object, e As EventArgs) Handles btLogin.Click
Dim hash as String
hash = HashPassword(txPass.text)
Dim sql As String = "SELECT user,password FROM fusion_login WHERE user = @user AND password = @pass"
Using myconnection As MySqlConnection = Connection.getInstance.getConnection()
Using mycommand As New MySqlCommand()
With mycommand
.CommandText = sql
.CommandType = CommandType.Text
.Connection = myconnection
.Parameters.Add("@user", MySqlDbType.VarChar).Value = txUser.Text
.Parameters.Add("@pass", MySqlDbType.VarChar).Value = hash
End With
Try
myconnection.Open()
myreader = mycommand.ExecuteReader
If myreader.HasRows = 0 Then
Me.Hide()
FusionPrincipal.Show()
Else
MessageBox.Show("Error", "Login", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txUser.Focus()
End If
Catch ex As MySqlException
MessageBox.Show("Error: " + ex.ToString)
Finally
myconnection.Close()
myreader.Close()
End Try
End Using
End Using
I'm not sure whether I'm inserting the password wrong, or comparing the username/password wrong at login. What could be the problem here?