试图验证用户名(电子邮件地址)和密码 - 有问题 - vb.net

时间:2017-04-06 11:21:20

标签: vb.net validation email passwords credentials

我有一个Windows窗体应用程序未正确验证用户输入信息。需要一些帮助。 我插入了Microsoft登录表单,我正在编写代码来验证用户凭据。使用Access DB存储和检索信息。两个表 - 一个用于电子邮件地址,另一个用于密码 我使用正则表达式验证电子邮件addy的格式。这非常有效。 我验证电子邮件地址的格式是否正确并验证它是否在表格中(这很有效)。然后我尝试读取密码(看起来这没有按预期工作),然后从表中读取两个信息位。接下来,我测试以确保两者都存在。如果两者都存在,则控制权被传递给另一种形式。

我的问题是阅读/验证密码。

这是我的Visual Studio VB.net代码。

Private Sub OK_Click(sender As System.Object, e As System.EventArgs) Handles OK.Click

    Try

        If MsgBox("Is your information correct?", MsgBoxStyle.YesNo, "M&P Records") = MsgBoxResult.Yes Then

            Dim pattern As String = "^[A-Z][A-Z|0-9|]*[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"

            Dim match As System.Text.RegularExpressions.Match = Regex.Match(txtUsername.Text.Trim(), pattern, RegexOptions.IgnoreCase)
            If (match.Success) Then


                Try
                    If i = 0 Then
                        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="

                        'Change the following to your access database location
                        dataFile = "\11_2017_Spring\CSCI-2999_Capstone\DB_M&PRecords.accdb"

                        connString = provider & dataFile
                        myConnection.ConnectionString = connString
                        myConnection.Open()
                        i = 1
                    End If

                Catch ex As Exception
                    '  An error occured!  Show the error to the user and then exit.
                    MessageBox.Show(ex.Message)
                End Try


                'the query:

                Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [EmailAddress] WHERE [emailAddress] = '" & txtUsername.Text & "'", myConnection)
                Dim com As OleDbCommand = New OleDbCommand("SELECT * FROM [Password] WHERE [Password] = '" & txtPassword.Text & "'", myConnection2)

                Dim dr As OleDbDataReader = cmd.ExecuteReader()
                Dim drp As OleDbDataReader = com.ExecuteReader()

                ' the following variable is hold true if EmailAddress is found, and false if EmailAddress is not found 
                Dim userFound As Boolean = False

                ' the following variable is hold true if Password is found, and false if Password is not found 
                Dim passwordFound As Boolean = False

                ' the following variables will hold the EmailAddress and Password if found.
                Dim EmailAddressText As String = ""
                Dim PasswordText As String = ""

                'if found:
                While dr.Read()
                    userFound = True
                    EmailAddressText = dr("EmailAddress").ToString
                End While

                While drp.Read()
                    passwordFound = True
                    PasswordText = drp("Password").ToString
                End While

                'checking the result
                If userFound = True And passwordFound = True Then
                    frmMain.Show()
                    frmMain.Label1.Text = "Welcome " & EmailAddressText & " "
                Else
                    MsgBox("Sorry, username or password not found", MsgBoxStyle.OkOnly, "M&P Records - Invalid Login")

                    With txtPassword
                        .Clear()
                    End With

                    With txtUsername
                        .Clear()
                        .Focus()
                    End With

                End If

            Else
                MessageBox.Show("Please enter a valid email address", "M&P Records - Email Check")

                With txtPassword
                    .Clear()
                End With

                With txtUsername
                    .Clear()
                    .Focus()
                End With
            End If

        End If

    Catch ex As Exception
        '  An error occured!  Show the error to the user and then exit.
        MessageBox.Show(ex.Message)
    End Try

End Sub

2 个答案:

答案 0 :(得分:0)

首先,您的方法并非真正安全,因为密码未加密,并且电子邮件和密码之间没有链接,理想情况下您将拥有表格,例如:

USER --uid --email

PASS - ID --uid --pass

为了更安全,您可以使用示例sha512散列密码,以便使用Salt和证书来保护数据库连接。

然后你可以这样做: 在文本框中哈希当前密码并执行:

var textNodes = $("#content *").contents().filter(function() {
    return this.nodeType === 3;
});

var counter = 1;
for(var i = 0;i<textNodes.length;i++)
{
    var val = $(textNodes).eq(i).text();
  var words = val.split(" ");
  var final = "";

  for(var j = 0;j<words.length;j++)
  {
    if(words[j].trim() != "")
    {
        final += "<span id='"+ counter +"'>"+ words[j] +" </span>";
      counter++;
    }
  }

  $($(textNodes)[i]).replaceWith(final);
}

如果您已连接,请检查您是否有结果。

但是我试着纠正你在上面的代码中所做的一些事情。我只使用了SQLClient而不是Olecommand我试图保留你所做的,所以语法错误可能很少,但应该没问题:

"SELECT USER.Email FROM USER,PASS WHERE USER.Email='TEXTBOX_EMAIL' AND USER.UID = PASS.UID"

答案 1 :(得分:0)

决定将电子邮件和密码合并到一个表中。让一切变得更加容易。感谢您的帮助和建议。