2014-06-11 31 views
0

好吧,我認爲這是相當自我解釋和有史以來最容易的事情。但出於某種原因,我無法讓這個工作。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網頁的資源可能不正確。至少,我不能這樣做。請參閱下面的答案和評論。

+0

在哪裏/如何定義NumberOfAttempts?在哪裏/如何定義「x」,它來自哪裏?你有沒有設置一個斷點來查看哪個部分不能按預期工作? – Plutonix

+0

您確定標籤中有文字,並且不會隱藏在其他控件後面? – UnhandledExcepSean

+0

這是一個網頁或Windows窗體? –

回答

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 

在上面的代碼檢查,看看會議有一個值,獲得該價值的關鍵部分(這將是默認爲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

+3

使用cookie可能不是最好的選擇,因爲這是跟蹤登錄嘗試並且客戶端可以更改cookie值,除非cookie由服務器加密/簽名。 – Mark