任何人都可以從代碼告訴我什麼是錯的代碼?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
題外話,但,東西要考慮你的代碼:** 1 **永遠'dispose'你的對象。 ** 2。**在關閉與數據庫的打開連接之前從不重定向。 ** 3。**總是把數據庫代碼放在它自己的方法中,如果可以的話,在它自己的層中。 **主題**'ex.Message()'的值是多少? – balexandre
除了balexandre,1.不要推出自己的安全。 2.不要以純文本形式存儲密碼。 – Thomas