VB.net登录尝试计数器

时间:2014-06-11 12:57:06

标签: vb.net

好吧我认为这是非常自我解释的,也是最简单的事情。但出于某种原因,我不能让这个工作。

Partial Class IntroductionPage 'CodeBehind for an ASPX Web page.
    Public NumberOfAttempts As Integer = 0
    Protected Sub PinButton_Click(sender As Object, e As System.EventArgs) Handles PinButton.Click
        NumberOfAttempts = NumberOfAttempts + 1
        'Query the database for the password, etc (omitted)...
        If (x = 1 And NumberOfAttempts <= 10) Then
            ' Then Login the user successfully. (omitted)
        Else
            ' The Pin was not found in the DB.  We should throw error and make the validation label visible. (omitted)
        End If
        If (NumberOfAttempts > 10) Then
            AttemptsErrorMessage.Visible = True
        End If
    End Sub
End Class

在测试中,我只是尝试使用错误的密码登录10次,但标签没有出现。我每次尝试不同的密码。此外,即使在10次尝试之后,我尝试了一个有效的密码,然后程序仍然成功地登录用户(根据第一个if语句的逻辑,这不应该有。)

我尝试关注此资源,以及其他一些描述完全相同过程的资源:How to count login attempts Visual Basic为将来的观看者编辑/注意:基本上,似乎该资源可能对ASPX网页不正确。至少,我无法以这种方式工作。请参阅下面的答案和评论。

2 个答案:

答案 0 :(得分:3)

每次在页面上发帖时,您都会收到一个新页面。 NumberOfAttempts永远不会达到10的值。您需要将值存储在Session变量,cookie,数据库或您有权访问的其他位置,并且每次都加载它。

有关页面生命周期的其他信息,请参阅此MSDN页面。 http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

这是一篇关于MSDN的文章,讨论了使用ASP.NET管理状态时的选项。 http://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx

答案 1 :(得分:3)

您的应用程序是一个Web应用程序,因此每次您按此行回发时,计数器的值将重置为0:

Public NumberOfAttempts As Integer = 0

您需要跟踪Session(或其他一些持久存储机制)中的尝试次数。尝试这样的事情:

Partial Class IntroductionPage 'CodeBehind for an ASPX Web page.

    Protected Sub PinButton_Click(sender As Object, e As System.EventArgs) Handles PinButton.Click

        Dim NumberOfAttempts As Integer

        If Session("NumberOfAttempts") Is Not Nothing Then        
            NumberOfAttempts = CInt(Session("NumberOfAttempts"))
        End If

        'Query the database for the password, etc (omitted)..

        NumberOfAttempts = NumberOfAttempts + 1
        Session("NumberOfAttempts") = NumberOfAttempts

        If (x = 1 And NumberOfAttempts <= 10) Then
            ' Then Login the user successfully. (omitted)
        Else
            ' The Pin was not found in the DB.  We should throw error and make the validation label visible. (omitted)
        End If

        If (NumberOfAttempts > 10) Then
            AttemptsErrorMessage.Visible = True
        End If
    End Sub
End Class

上面代码中的关键部分是检查Session是否有值并获取该值(默认情况下它将为0),然后在每次用户尝试时递增该计数(并将其放回Session中)登录。

相关问题