2013-04-22 174 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