OK. When I add a user I have this:-
Code:
Dim salt As String = CreateSalt(8)
Dim passwordHash As MySqlParameter = mycomm.Parameters.Add("passwordHash", MySqlDbType.VarChar)
passwordHash.Value = CreatePasswordHash(user.password, salt)
Dim psalt As MySqlParameter = mycomm.Parameters.Add("salt", MySqlDbType.VarChar)
psalt.Value = salt
CreateSalt
Code:
Public Shared Function CreateSalt(ByVal size As Integer) As String
' Generate a cryptographic random number using the cryptographic
' service provider
Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider
Dim buff As Byte() = New Byte(size) {}
rng.GetBytes(buff)
' Return a Base64 string representation of the random number
Return Convert.ToBase64String(buff)
End Function
CreatePasswordHash
Code:
Public Shared Function CreatePasswordHash(ByVal pwd As String, ByVal salt As String) As String
Dim saltandPwd As String = String.Concat(pwd, salt)
Dim hashedPwd As String = FormsAuthentication.HashPasswordForStoringInConfigFile(saltandPwd, "SHA1")
hashedPwd = String.Concat(hashedPwd, salt)
Return hashedPwd
End Function
Then to check the password I have
Code:
Public Shared Function CheckLogin(ByVal email As String, ByVal password As String) As UserBOL
Dim result As New UserBOL
Dim conn As New MySqlConnection(ConnDAL.connString)
Dim mycomm As New MySqlCommand("SELECT userref,salt,passwordHash FROM users WHERE email=?email", conn)
Dim rs As MySqlDataReader
Using conn
Using mycomm
Try
mycomm.Parameters.AddWithValue("email", email)
conn.Open()
rs = mycomm.ExecuteReader
If rs.HasRows Then
rs.Read()
If CreatePasswordHash(password, rs("salt")) = rs("passwordHash") Then
'authenticated
result.userref = rs("userref")
Else
'It always ends up here
result.userref = 10
result.email = CreatePasswordHash(password, rs("salt"))
End If
Else
result.userref = 9
End If
rs.Close()
Catch ex As Exception
result.err = ex.ToString
result.userref = 11
Finally
mycomm.Dispose()
conn.Close()
conn.Dispose()
End Try
End Using
End Using
Return result
End Function
Bookmarks