我需要有关此的帮助,为此我一直在安静地工作... 我想使用一个登录表单同时为admin和普通用户登录,但是登录后,其某些表单和/或表单中的按钮对于普通用户不可用。 这是我登录时的代码:
If txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("Please fill in all fields.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
cn.ConnectionString = "Server=localhost; User Id=root; Password=''; Database=omt"
Try
Dim sql As String = "SELECT Username, Password, Position FROM tbl_account WHERE Username='" & txtUsername.Text & "' AND Password='" & txtPassword.Text & "'"
cmd = New MySqlCommand(sql, cn)
cmd.Connection = cn
cn.Open()
Dim dr As MySqlDataReader = cmd.ExecuteReader
If dr.Read = True Then
sql = ("SELECT Position FROM tbl_account WHERE Position = 'Admin'")
Me.Hide()
MDIParent1.Show()
ElseIf sql = ("SELECT Position FROM tbl_account WHERE Position = 'User'") Then
Me.Hide()
MDIParent1.Show()
Else
MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show("Failed to connect to databse. System Error:" & ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
If cn.State <> ConnectionState.Closed Then
cn.Close()
End If
End If
End If
答案 0 :(得分:0)
我认为,您应该认真考虑修改身份验证机制。在数据库表中存储密码(更不用说以明文形式存储)不是一个好习惯。
话虽如此,您可以像以下这样获得成功通过身份验证的用户的位置(假设它存储在非空字符数据类型列中,例如varchar(n)):
Dim position As String = Nothing
If dr.Read = True Then
position = dr("Position")
Select Case position
Case "Admin"
' TODO: Handle admin
Case "User"
' TODO: Handle user
Case Else
' TODO: Neither Admin nor User. Now what?
End Select
Else
MessageBox.Show("Incorrect Username or Password.",
"Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
然后,您可以根据需要处理每种情况(显示/隐藏正确的表单,启用或禁用表单控件等)。 请原谅任何语法错误,因为我目前无法使用Visual Studio。
答案 1 :(得分:0)
将数据库对象保留在使用它们的方法本地。您可以确保将其关闭并丢弃。 Using...End Using
块即使有错误也可以为您处理。
我填写了DataTable
,而不是直接使用阅读器。关闭连接后,我可以使用DataTable
中的数据。阅读器处于活动状态时,连接必须保持打开状态。尽快关闭连接非常重要。关闭连接后,您可以在显示消息框中检查数据。
始终使用参数来避免Sql注入。服务器将作为命令文本一部分传递的文本视为可执行代码。传递给参数的值不可执行。
当然,永远不要将密码存储为纯文本。我将留给您研究盐腌和哈希。
Private Sub OPCode()
If txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("Please fill in all fields.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Dim dt As New DataTable
Try
Using cn As New MySqlConnection("Server=localhost; User Id=root; Password=''; Database=omt"),
cmd As New MySqlCommand("SELECT Username, Password, Position FROM tbl_account WHERE Username= @User AND Password= @Password;", cn)
cmd.Parameters.Add("@User", MySqlDbType.Text).Value = txtUsername.Text
cmd.Parameters.Add("@Password", MySqlDbType.Text).Value = txtPassword.Text
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using 'Closes and disposes the connection and the command
If dt.Rows.Count = 1 Then
If dt(0)(2).ToString = "Admin" Then
'Do what you need for Admin
ElseIf dt(0)(2).ToString = "User" Then
'Do what you need for User
Else
MessageBox.Show("Position doesn't match existing values.")
Return
End If
MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Me.Hide()
MDIParent1.Show()
Catch ex As Exception
MessageBox.Show("Failed to connect to databse. System Error:" & ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub