温馨提示:本文翻译自stackoverflow.com,查看原文请点击:asp.net - block input textbox for 1 hour
asp.net vb.net

asp.net - 阻止输入文本框1小时

发布于 2020-03-29 21:28:37

自昨天以来,我有一个要解决的问题。我知道如何阻止用户文本框输入很简单,但是现在我想在特定时间(1小时)内执行此操作。用户输入密码超过3次的情况。

这意味着我需要创建一个倒数计时,这是我受阻的地方。因此,我尝试使用计时器。但是,当我使用TextBoxTimer_Tick方法中的断点调试程序时,永远不会触发此事件。

这是我做的一些代码:

Private TextBoxTimer As New Timer()
Dim TextboxCompteur As Integer

Private Sub TbxUtilisateur_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Handles TbxUtilisateur.TextChanged
    AddHandler TextBoxTimer.Tick, AddressOf TextBoxTimer_Tick
    TextBoxTimer.Interval = 1000 'Every 1 seconde
    TextBoxTimer.Enabled = True
    TextboxCompteur = 10 'during 10 secondes the textbox will be blocked for test
    .
    . some other code
    .
End Sub

Protected Sub TextBoxTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        TextboxCompteur -= 1
        If Not TextboxCompteur = 0 Then Exit Sub

        TextBoxTimer.Enabled = False
        TextBoxTimer.Dispose()
        RemoveHandler TextBoxTimer.Tick, AddressOf TextBoxTimer_Tick

        TbxMotDePasse.ReadOnly = False
End Sub

也许有人会看到我想念的?否则,如果您对如何处理这个问题有其他想法,我在听

感谢您将来的回答!

查看更多

提问者
naecotor
被浏览
118
Caius Jard 2020-01-31 18:18

您不能在这样的Web应用程序中轻松使用计时器;您应该实施一个解决方案,在您决定将用户锁定一小时时,更新一个数据库列,该列将用户锁定到当前时间之后一小时。这是伪代码:

bool Login(string username, string password){

  User x = DoDbLookupForUser(username)

  //user table has USERNAME, HASHEDPASSWORD, WRONGATTEMPTSCOUNTER, LOCKEDOUTUNTILDATE columns

  if x == null
    throw new UserNotFoundException

  if DateTime.Now < x.LockedOutUntilDate
    throw new UserLockedOutException

  if x.HashedPassword != Hash(password){
    x.WrongAttemptsCounter++

    if x.WrongAttemptsCounter >= 3
      x.LockedOutUntilDate = DateTime.Now.AddHours(1)

    SaveUser(x)
    throw new UserPasswordIncorrectException
  }

  x.WrongAttemptsCounter = 0;
  SaveUser(x)

  return true
}

3次尝试后,用户被锁定。他们可以在一小时后重试。如果他们再次出错,则将被锁定另一个小时。如果要在一小时内尝试3次,请在设置日期时(而不是成功登录时)将计数器重置为0。无需将锁定日期重置为null。一旦过去,它将被忽略。知道用户是否曾经将自己锁定在什么地方以及何时保持数据锁定可能很方便