如果用户名有3个attmept,如何阻止用户名? [英] how to block username if the username have a 3 attmept?
本文介绍了如果用户名有3个attmept,如何阻止用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Private Sub CmdAdminLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdminLogin.Click
If Len(Trim(TxtAdminUserName.Text)) = 0 Then
MessageBox.Show("Please enter user name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TxtAdminUserName.Focus()
Exit Sub
End If
If Len(Trim(TxtAdminPassword.Text)) = 0 Then
MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TxtAdminPassword.Focus()
Exit Sub
End If
Try
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\DbCPDRAS.accdb;Persist Security Info=False;")
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
Dim dr1 As OleDbDataReader
Dim com As New OleDbCommand
com.CommandText = "select [UserID],[Pass] from AdminInfo where UserID = @UName"
' UserName
Dim UName As OleDbParameter = New OleDbParameter("@UName", OleDbType.VarWChar, 20)
UName.Value = UCase(TxtAdminUserName.Text.ToString())
com.Parameters.Add(UName)
com.Connection = cn
dr1 = com.ExecuteReader
If dr1.Read Then
If UCase(dr1("Pass")) = UCase(TxtAdminPassword.Text) Then
cn.Close()
CBformState.SelectedItem = "Admin"
Dim obj As New FrmMain
Me.Hide()
obj.Show()
Else
Static Counter As Integer = 0
MsgBox("Invalid Password " & 2 - Counter & " Attempts left!", vbExclamation, "Login")
TxtAdminPassword.Text = ""
TxtAdminUserName.Text = ""
Counter += 1
If Counter = 3 Then Me.Close()
cn.Close()
LinkLabel2.Visible = True
LinkLabel2.Text = "Forget Password"
Return
End If
Else
Static Counter1 As Integer = 0
MsgBox("Invalid UserName and Password " & 2 - Counter1 & " Attempts left!", vbExclamation, "Login")
TxtAdminPassword.Text = ""
TxtAdminUserName.Text = ""
Counter1 += 1
If Counter1 = 3 Then
Me.Timer1.Interval = 1
Me.Timer1.Start()
CmdAdminLogin.Enabled = False
TxtAdminUserName.Enabled = False
TxtAdminPassword.Enabled = False
If Me.Timer1.Interval = 3 Then
Me.Timer1.Stop()
MessageBox.Show("Login Available", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)
CmdAdminLogin.Enabled = True
TxtAdminUserName.Enabled = True
TxtAdminPassword.Enabled = True
cn.Close()
End If
Return
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
推荐答案
对于单个应用程序会话:
保持一个包含尝试次数的类级整数。在每次失败时,将此数字增加1.当3次尝试结束时,将帐户锁定在数据库中并添加条件并显示相应的消息。
对于多会话:
在每次失败时,更新用户在数据库中的失败尝试。如果此数字超过3,则显示帐户锁定消息。
更新:以下是如何实现此功能的大纲。它在C#中,但逻辑在VB中也是一样的。
在单个应用程序会话中:
For single application session:
Keep a class level integer that holds number of tries. On each failure increase this number by 1. When 3 tries are over, lock the account in database and add a condition do display appropriate message.
For multi-session:
On each failure, update failed attempts in database for the user. If this number goes above 3, display account locked message.
Update: Here is an outline on how to implement this. It is in C# but logic remains same in VB as well.
In single application session:
private int _failedAttempts;
protected void LoginButtonClick(object sender, EventArgs e) {
// Try user login here
if (success)
{
// process ahead
}
else {
_failedAttempts++;
}
if (_failedAttempts == 3) {
// Update database for this user. Set account locked out.
MessageBox.Show("Your account has been locked out.");
}
}
多个会话:
Multiple session:
private int _failedAttempts;
protected void LoginButtonClick(object sender, EventArgs e)
{
// User table should have a failed attempt count field.
// Check if value of that field is 3
if (valueIs3)
{
MessageBox.Show("Your account has been locked out.");
}
else
{
// Try user login here
if (success)
{
// set failed attempt field value as 0
// process ahead
}
else
{
_failedAttempts = failedAttemptCountValueFromDatabase;
// add one to failed attempts field
_failedAttempts++;
}
if (_failedAttempts == 3)
{
// Update database for this user. Set account locked out.
MessageBox.Show("Your account has been locked out.");
}
}
}
这篇关于如果用户名有3个attmept,如何阻止用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文