2012-05-05 53 views
0

任何人都可以从代码告诉我什么是错的代码?VB.NET - ASP.NET - 不正确的用户名/密码(验证)

如果用户名和密码不匹配,lbl文本应显示“不正确的用户名/密码”。

代码:

Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click 

     Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb") 
     Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [User] where Username=? and Password=?", conn) 

     cmd.Parameters.AddWithValue("@Username", txtLogin.Text) 
     cmd.Parameters.AddWithValue("@Password", txtPassword.Text) 




     If (String.IsNullOrEmpty(txtLogin.Text)) Or (String.IsNullOrEmpty(txtPassword.Text)) Then 

      lblLoginError.Text = "One or more fields are empty. Please fill in all the fields" 
      lblLoginError.Visible = True 

     Else 

      conn.Open() 
      Dim read As OleDbDataReader = cmd.ExecuteReader() 

      Try 

       If read.HasRows Then 

        While read.Read() 

         If txtLogin.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then 


          Dim tUsername As String = read.Item("Username").ToString 

          Session("Username") = tUsername 
          Response.Redirect("Default.aspx") 


         End If 
        End While 
       End If 

       read.Close() 
      Catch ex As Exception 
       Response.Write(ex.Message()) 
       lblLoginError.Text = "Incorrect Username/Password." 
       lblLoginError.Visible = True 

      Finally 
       conn.Close() 
      End Try 


     End If 

    End Sub 
+0

题外话,但,东西要考虑你的代码:** 1 **永远'dispose'你的对象。 ** 2。**在关闭与数据库的打开连接之前从不重定向。 ** 3。**总是把数据库代码放在它自己的方法中,如果可以的话,在它自己的层中。 **主题**'ex.Message()'的值是多少? – balexandre

+0

除了balexandre,1.不要推出自己的安全。 2.不要以纯文本形式存储密码。 – Thomas

回答

1

你可以试试这个代码。此代码没有TryCatch块。

Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click 

     If (String.IsNullOrEmpty(txtLogin.Text)) Or (String.IsNullOrEmpty(txtPassword.Text)) Then 
      lblLoginError.Text = "One or more fields are empty. Please fill in all the fields" 
      lblLoginError.Visible = True 

     Else 
      Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb") 
      Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [User] where Username=? and Password=?", conn) 
      cmd.Parameters.AddWithValue("@Username", txtLogin.Text) 
      cmd.Parameters.AddWithValue("@Password", txtPassword.Text) 
      conn.Open() 
      Dim read As OleDbDataReader = cmd.ExecuteReader()  
        If read.HasRows Then 
         read.Read() 
         Session("Username") = read.Item("Username").ToString 
         read.Close() 
         conn.Close() 'Close connection before Redirecting. 
         Response.Redirect("Default.aspx")  
        Else 
         read.Close() 
         conn.Close() 
         lblLoginError.Text = "Incorrect Username/Password." 
         lblLoginError.Visible = True 

        End If 
      End If 
     End Sub 
+0

,这是行不通的。它仍然显示第一个错误信息! – Brian

+0

我的意思是“一个或多个字段为空,请填写所有字段”。无论我做什么,它仍然表明一个。 – Brian

+1

lblLoginError.Visible = false; 尝试把这个在你的page_load(如果你还没有) – Thousand

2

取而代之的是catch的写Else的if语句

+0

在您将其更改为内部If语句之前,我已将您的答案标记为有用。 ITYM If语句检查read.HasRows:如果没有匹配的条目,它将不会有行。 –

+0

@AndrewMorton Yeh,你将不得不同时检查。这就是为什么我把它改为“if语句” – Magnus

0

你写它的方式,“不正确的用户名/密码”将仅抛出一个异常表现。

,如果你想使用的代码为你写它,添加一个ELSE:

If txtLogin.Text = read.Item("username").ToString And txtPassword.Text = read.Item("password").ToString Then 


         Dim tUsername As String = read.Item("Username").ToString 

         Session("Username") = tUsername 
         Response.Redirect("Default.aspx") 
else 
throw new exception("Incorrect Username/Password") 
End If 
1

您不必从数据库中返回用户名和密码,因为您已经拥有它们。你只需要计算匹配的条目。这大大简化了它。此外,作为果酱表明,最好做什么用的数据库做之前做的用户名和密码字段的值测试:

If (String.IsNullOrEmpty(txtLogin.Text)) OrElse (String.IsNullOrEmpty(txtPassword.Text)) Then 

    lblLoginError.Text = "One or more fields are empty. Please fill in all the fields" 
    lblLoginError.Visible = True 

Else 

    Dim ok As Integer = 0 

    Using conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\PetLandia\App_Data\db.mdb") 
     Dim cmd As OleDbCommand = New OleDbCommand("SELECT COUNT(*) FROM [User] where Username=? and Password=?", conn) 

     cmd.Parameters.AddWithValue("@Username", txtLogin.Text) 
     cmd.Parameters.AddWithValue("@Password", txtPassword.Text) 

     conn.Open() 
     ok = CInt(cmd.ExecuteScalar()) 
     conn.Close() 
    End Using 

    If ok = 0 Then 
     ' credentials incorrect 
    Else 
     ' credentials correct 
    End If 

End If 
+0

与张贴的内容相同的问题。我只是用正确的 – Brian

+0

@Brian替换了不正确的重定向标签,然后txtLogin.Text或txtPassword.Text为空或空。这些控件是在运行时生成的吗? –

+0

不,当按钮被点击时,我猜 – Brian

0
  1. 您决定推出自己的安全而导致..
  2. 您似乎以明文存储密码,这是一个巨大的安全漏洞和潜在的责任来源。
  3. If read.HasRows将在数据库中不存在传递的用户名和密码时为false。也就是说,它不会抛出异常,它只会返回没有行。
  4. 你没有拨打Dispose上的一次性物品。
  5. Select Count(*)简单地调用ExecuteScalar来查看结果是否大于零将会更快。

Dim authenticationFailed As Boolean = String.IsNullOrEmpty(txtLogin.Text) _ 
    OrElse String.IsNullOrEmpty(txtPassword.Text) 

If Not authenticationFailed Then 
    Dim connString = "Provider=Microsoft.Jet.OLEDB.4.0..." 
    Using conn = New OleDbConnection(connString) 
     Const sql As String = "Select Count(*) From [User] Where Username=? and Password=?" 
     conn.Open() 
     Using cmd = New OleDbCommand(sql, conn) 
      cmd.Parameters.AddWithValue("@Username", txtLogin.Text) 
      cmd.Parameters.AddWithValue("@Password", txtPassword.Text) 

      Try 
       Dim result = cmd.ExecuteScalar(CommandBehavior.CloseConnection) 
      Catch generatedExceptionName As SqlException 
       authenticationFailed = True 
      End Try 

      authenticationFailed = authenticationFailed _ 
       OrElse Convert.ToInt32(result) <> 1 

      If Not authenticationFailed Then 
       Session("Username") = txtLogin.Text 
      End If 
     End Using 

     conn.Close() 
    End Using 
End If 

If authenticationFailed Then 
    lblLoginError.Text = "Incorrect username and password" 
    lblLoginError.Visible = True 
End If