2013-04-22 178 views
-3

我的登錄系統出現問題,在使用第一個用戶(管理員)時尋求一些極客幫助系統做它需要做的事情。但是,當我嘗試用不同的用戶登錄時,它將無法工作。我讓我的錯誤username and password unknown,當我刪除代碼,我可以與其他所有用戶的登錄以下行,VB中的登錄系統問題

ElseIf (currentUser <> username AndAlso currentPassword <> password) Then 
     MessageBox.Show("Username and password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
Return False 

源代碼,

Public Function Login(ByVal username As String, ByVal password As String) 
    Dim usersDatasSet As New DataSet() 
    usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users") 
    usersDataAdapter.Fill(usersDatasSet, "Users") 
    Dim table As DataTable = usersDatasSet.Tables("Users") 

    For i As Integer = 0 To table.Rows.Count - 1 
     Dim currentUser As String = table.Rows(i)("Username").ToString().Trim() 
     Dim currentPassword As String = table.Rows(i)("Password").ToString().Trim() 


     'Check input 

     If (currentUser <> username And currentPassword = password) Then 
      MessageBox.Show("Unknown user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      Return False 

     ElseIf (currentUser = username And currentPassword <> password) Then 
      MessageBox.Show("Wrong password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      Return False 


     ElseIf (currentUser <> username AndAlso currentPassword <> password) Then 
      MessageBox.Show("Username and password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      Return False 

     ElseIf (currentUser = username AndAlso currentPassword = password) Then 
      usersDatasSet.Dispose() 
      Connection.Close() 
      Return True 
     End If 

    Next 
    usersDatasSet.Dispose() 
    Connection.Close() 
    Return False 
End Function 

感謝在這個問題上的任何幫助。

+0

您使用的是ASP.NET提供的數據庫還是您自己的數據庫? – Zeddy 2013-04-22 22:43:18

+0

什麼是記錄*到*? – 2013-04-22 23:10:47

+0

你爲什麼要遍歷用戶表中的行?你應該根據用戶名選擇1行,而不是整個表。 – Tim 2013-04-23 04:58:30

回答

2

您正在循環查看錶中的行,並根據用戶提供的用戶名和密碼值檢查每行的值。

既然你是第一個用戶是管理員,管理員將永遠能夠登錄。

由於您的ElseIf (currentUser <> username AndAlso currentPassword <> password) Then塊(您嘗試使用John登錄,但您使用的是管理員憑據),因此任何其他用戶都將失敗。

當您刪除ElseIf (currentUser <> username AndAlso currentPassword <> password) Then任何用戶都可以登錄 - 因爲他們實際上是以管理員身份登錄的。

嘗試從表中選擇用戶並將用戶名和密碼與提供的值進行比較。

你應該做的

爲了驗證用戶可以登錄,你可以做以下的(而不是你的環):

Public Function Login(ByVal username As String, ByVal password As String) As Boolean 

    ' Set a flag for whether or not login was successful 
    Dim LoggedIn As Boolean = False 
    Dim usersDatasSet As New DataSet() 
    usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users") 
    usersDataAdapter.Fill(usersDatasSet, "Users") 
    Dim table As DataTable = usersDatasSet.Tables("Users") 

    ' This will return an array of DataRows that have the specified 
    ' username in them. 
    ' You will need to have unique usernames for this to work 
    Dim credentials() As DataRow = table.Select("Username = '" + username + "'") 

    If (credentials.Length = 1) Then 
     Dim currentUser As String = credentials(0)("Username").ToString() 
     Dim currentPassword As String = credentials(0)("Password").ToString() 

     If (currentUser <> username And currentPassword = password) Then 
      MessageBox.Show("Unknown user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     ElseIf (currentUser = username And currentPassword <> password) Then 
      MessageBox.Show("Wrong password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     ElseIf (currentUser <> username AndAlso currentPassword <> password) Then 
      MessageBox.Show("Username and password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     ElseIf (currentUser = username AndAlso currentPassword = password) Then 
      LoggedIn = True 
     End If 
    Else 
     MessageBox.Show("Mulitple users found for " & username, "Error", MessageBox.Buttons.OK, MessageBox.Icon.Error) 
    End If 

    usersDatasSet.Dispose() 
    Connection.Close() 

    Return LoggedIn 
End Function 

這將允許你告訴用戶它是否是驗證失敗的用戶名或密碼,並處理是否有多個具有相同用戶名的用戶。

然而

我會鼓勵你使用這樣的系統(其中講述了登錄的一部分失敗的用戶),因爲它可能給黑客信息,如果他們試圖強力攻擊。 (是的,我知道,這裏可能有點偏執)。

這將是更好地簡化這樣的:

Public Function Login(ByVal username As String, ByVal password As String) As Boolean 

    ' Set a flag for whether or not login was successful 
    Dim LoggedIn As Boolean = False 
    Dim usersDatasSet As New DataSet() 
    usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users") 
    usersDataAdapter.Fill(usersDatasSet, "Users") 
    Dim table As DataTable = usersDatasSet.Tables("Users") 

    ' This will return an array of DataRows that have the specified 
    ' username in them. 
    ' You will need to have unique usernames for this to work 
    Dim credentials() As DataRow = table.Select("Username = '" + username + "' AND Password = '" + password + "'") 

    If (credentials.Length =1) Then 
     LoggedIn = True 
    Else 
     MessageBox.Show("Invalid username/password combination", "Error", MessageButtons.OK, MessageBoxIcon.Error) 
    End If 

    usersDatasSet.Dispose() 
    Connection.Close() 

    Return LoggedIn 
End Function 

現在,一個更好的方法和更安全將有以下要素:

  1. 密碼散列(用鹽)並存儲在您的數據庫中。 1a。您必須使用用戶輸入的純文本密碼進行哈希(使用正確的salt),並將其與表中存儲的哈希進行比較以獲取用戶名,以便他們成功登錄。
  2. 限制嘗試次數 - if他們超過了嘗試的次數(例如說3),賬戶被鎖定。這可以防止暴力攻擊。
  3. 對錶使用參數化查詢來防止SQL注入攻擊。我意識到這很可能是一個WinForms應用程序(基於對MessageBox的調用),但參數化查詢是一個很好的編程習慣。
+0

感謝您的回覆和有用的提示。 – user2309143 2013-04-23 07:15:12