任何人都可以从代码告诉我什么是错的代码?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