我试图弄清楚这些逻辑。例如:如果用户输入了3次不正确的值,他将收到一条消息,如尝试过多,并且10秒钟之内不能键入任何内容,此后他应该可以再次登录。我是vb.net的初学者,有点在这里迷路了。我希望有人能够帮助我解决我的问题
PS:我认为在我的逻辑中可能的方法是使用Timer秒,但不知道如何在我的代码下构造它
Private Sub btn_login_Click(sender As Object, e As EventArgs) Handles btn_login.Click
Dim command As New SqlCommand("Select * from user_access where Username= @user AND Password= @pass", con)
command.Parameters.Add("@user", SqlDbType.VarChar).Value = txt_username.Text
command.Parameters.Add("@pass", SqlDbType.VarChar).Value = txt_password.Text
con.Open()
reader = command.ExecuteReader()
Dim count As Integer = 0
While (reader.Read())
count = count + 1
End While
If (count = 1) Then
MessageBox.Show("Login Successfully")
LoginForm1.Hide()
MainForm.ShowDialog()
Else
MessageBox.Show("Invalid Username/Password")
End If
If count = 3 Then
MessageBox.Show("Too Many Logins")
End If
con.Close()
End Sub
答案 0 :(得分:0)
通过设计器或在类级别向代码添加计时器,并将其间隔设置为10000(10s)。
在第3次失败时激活计时器,如下所示:
If count = 3 Then
MessageBox.Show("Too Many Logins. Disabled for 10s")
btn_login.Enabled = False
LockoutTimer.Start()
End If
然后您需要处理计时器完成:
Private Sub LockoutTimer_Tick(sender As Object, e As EventArgs) Handles LockoutTimer.Tick
LockoutTimer.Stop()
btn_login.Enabled = True
count = 0
End Sub
正如muffi在评论中提到的那样,您的逻辑已无法检查登录。当前,您检查有多少用户匹配给定的用户名和密码组合。如果登录失败,则需要检查是否存在并增加一个单独的变量:
Dim loginSuccess As Boolean = False
While (reader.Read())
loginSuccess = True
End While
If (loginSuccess) Then
count = 0
MessageBox.Show("Login Successfully")
LoginForm1.Hide()
MainForm.ShowDialog()
Else
count += 1
If count = 3 Then
MessageBox.Show("Too many invalid login attempts. Disabled for 10s")
btn_login.Enabled = False
LockoutTimer.Start()
Else
MessageBox.Show("Invalid Username/Password")
End If
End If
注意:count变量需要在单击按钮之间保持不变,因此请使其成为类中的一个字段,并在成功登录后重置(如果需要)
Private count As Integer = 0