Try
Dim connString As String = ConfigurationManager.ConnectionStrings("dbx").ConnectionString
Dim cmdString As String = "SELECT * FROM Users WHERE UserName = @UserName AND Password = @Password"
Using conn As New OleDbConnection(connString)
Using cmd As New OleDbCommand(cmdString, conn)
conn.Open()
cmd.Parameters.AddWithValue("@UserName", TextBox1.Text)
cmd.Parameters.AddWithValue("@Password", TextBox2.Text)
Dim reader As OleDbDataReader = cmd.ExecuteReader
dtRowsReturned.Load(reader)
End Using
End Using
If dtRowsReturned.Rows.Count > 0 Then
Me.Hide()
Dim dss As New Form1()
dss.ShowDialog()
Else
MessageBox.Show("Account/Password is incorrect Please try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As ApplicationException
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End Try
End Sub
Private Function IsValidated() As Boolean
If TextBox1.Text.Trim = String.Empty Then
MessageBox.Show("Account Required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Focus()
Return False
End If
If TextBox2.Text.Trim = String.Empty Then
MessageBox.Show("Passwrd Requried.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Focus()
End If
Return False
End Function
答案 0 :(得分:5)
我们看不到第一种方法的开始,但是基于我们所看到的,我猜测它会在继续之前检查IsValidated()
函数。 IsValidated()
在任何地方都不会返回True
,因此第一种方法的其余部分甚至都不会尝试运行密码检查。 IsValidated()
应该看起来像这样:
Private Function IsValidated() As Boolean
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
MessageBox.Show("Account Required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Focus()
Return False
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
MessageBox.Show("Passwrd Requried.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Focus()
Return False
End If
Return True
End Function
虽然我在这里,但从不可以直接将密码存储在数据库列中。您需要为每个用户的盐值添加一列。当用户设置新密码时,您可以在密码前添加salt值,然后对结果运行加密哈希(例如 BCrypt )。然后,您可以将该哈希值存储在数据库中。当某人尝试登录时,您可以在尝试输入的密码前面加上盐,然后运行相同的加密哈希算法,然后将结果与数据库中存储的哈希值进行比较。少了什么真的是不安全和业余的。
那仅仅是开始。还需要考虑内存安全性(即:SecureString
),传输安全性,重置过程等。简而言之,您不想自己编写此代码,也不希望以任何方式来构建自己的身份验证系统。获得结果是如此容易,以至于似乎是正确的-甚至通过了严格的单元测试,但是以微妙的方式是错误的,以至于一年后您发现自己在六个月前被黑了。相反,请尽可能使用您选择的平台已提供的身份验证工具。