无法验证密码 [英] Having Trouble validating password
问题描述
我从数据库中提取用户名和密码,并与输入到文本框中的值进行比较。
这是我正在处理的代码:
I am pulling username and password from a database and comparing to values entered into a text box.
Here is the code I'm working on:
Dim con As System.Data.SqlClient.SqlConnection
Dim cmd As System.Data.SqlClient.SqlCommand
Dim dr As System.Data.SqlClient.SqlDataReader
Dim sqlStr As String
Dim username As String
Dim password As String
Dim userid As Integer
Dim testme As String
Private Sub LoginTest ()
If usernamebox.Text.Length = 0 Then
MsgBox("You must enter a username")
usernamebox.Focus()
Exit Sub
ElseIf passwordbox.Text.Length = 0 Then
MsgBox("You must enter a password")
passwordbox.Focus()
Exit Sub
End If
con = New SqlClient.SqlConnection(Module1.GetConnectionString())
con.Open()
sqlStr = "Select userid, username, password from users where username = '" & usernamebox.Text & "';"
cmd = New SqlClient.SqlCommand(sqlStr, con)
dr = cmd.ExecuteReader()
If dr.HasRows Then
dr.Read()
userid = dr.Item("userid")
username = dr.Item("username")
password = dr.Item("password")
Else
MsgBox("Invalid Login Name")
Exit Sub
End If
If passwordbox.Text = password Then
MsgBox("Show roles Page")
Else
passwordbox.Text = ""
' The user name/password is invalid.
MsgBox("Password invalid")
passwordbox.Focus()
Exit Sub
End If
MsgBox("Show roles Page")
con.Close()
End Sub
总是没有比较 - 如果passwordbox.text =密码 - 即使它们是正确的。在调试模式的VB工作室中,我将鼠标悬停在密码上,并在实际密码之前显示双引号,如下所示:假设密码为mypassword,则从数据库中提取的值的悬停超过mypassword并且悬停的值超过在文本框上是mypassword。
我做错了什么?
It always fails the comparison - if passwordbox.text = password - even if they are correct. In VB studio in debug mode I hover over the password and it shows a double quote before the actual password like this: lets say the password is mypassword then the hover over of the value pulled from the db is "mypassword and the value from hover over on the textbox is "mypassword".
What am I doing wrong?
推荐答案
做密码在数据库中可能包含尾随空格? - 我感觉它的VB环境在值周围显示双引号,所以我能想到看到左边的而不是右边的唯一原因是,正确的是'离开视野'
从数据库中选择一些数据,在密码值附近添加['和']'之类的内容,并检查 - 你可能需要更新密码栏和'修剪'无关的空格(或其他sh * t)
你可以试试
does the password in the database contain trailing spaces perhaps ? - I have a feeling its the VB environment that displays the double quotes around the value, so the only reason I can think of to see the left one and not the right one, is, the right one is 'off the field of view'
do a select of data from the database, append something like '[' and ']' around the password value, and check - you may need to update the password column and 'trim' extraneous spaces (or other sh*t)
[edit] you could try
Select userid, username, rtrim(password) from users where username =
你的sql中的
(注意密码周围的rtrim() - 修剪空白字符串的权利)
如果它有效,那么......显然最好的答案是不要在那里有空格,但.... 。
[/ edit]
in your sql (note the rtrim() around the password - trims whitespace on the right of the string)
If it works, well .. obviously the best answer is not to have the whitespace there in the first place, but .....
[/edit]
这篇关于无法验证密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!