2017-04-06 3 views
0

ユーザー入力情報を適切に検証していないWindowsフォームアプリケーションがあります。助けが要る。 私はMicrosoftログインフォームを挿入し、ユーザーの資格情報を確認するためのコードを書いています。アクセスDBを使用して情報を格納および取得する。メールアドレス用とパスワード用の2つのテーブル。
私は正規表現を使用して電子メールaddyの形式を確認します。これは非常にうまくいく。 電子メールアドレスが正しい形式であることを確認し、それがテーブルにあることを確認します(これはうまくいきます)。次に、パスワードを読み取ろうとしています(これは期待どおりに動作していないようです)。次に、私は両方が存在することを確認するためにテストします。両方が存在する場合、コントロールは別のフォームに渡されます。ユーザー名(電子メールアドレス)とパスワードを検証しようとしています - 問題がある - vb.net

私の問題はパスワードの読み取り/確認です。

ここは私の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]+)*)[email protected][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 

答えて

0

まあ最初のアプローチは、パスワードが暗号化され、メールアドレスとパスワードの間にリンクがない理想的にあなたが例えばテーブルを持っているでしょういずれかのことをされていないため、事実に本当に安全ではありません。

USER --uid --email

PASS --id --uid --pass

そして、あなたはハッシュうあなたのパスワードsha512さらにセキュリティを強化するために、Saltと証明書を使用してデータベース接続を保護します。

次にあなたができる: テキストボックスに現在のパスワードをハッシュと実行:はい、あなたの接続されている場合、あなたが結果した場合

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

チェックを。

しかし、上記のコードで行ったことを少し修正しようとしました。唯一のSqlClientを使用し、私はあなたが非常に少ない構文エラーがあるかもしれませんでしたが、[OK]をする必要があり何維持しようとしたOlecommand持たない:

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]+)*)[email protected][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 
      Dim passwordFound As Boolean 
      Dim userFound As Boolean 
      Using con As New SqlClient.SqlConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =\ 11_2017_Spring\CSCI-2999_Capstone\DB_M&PRecords.accdb") 
       'Using to make sure connection is disposed 
       'Open connection 
       con.Open() 
       'Prepare sql 
       Dim command As New OleDbCommand("SELECT [emailAddress] FROM [EmailAddress] WHERE [emailAddress] = '" & txtUsername.Text & "';", con) 
       'Create the reader 
       Dim reader As OleDbDataReader = command.ExecuteReader() 
       Dim Id As String = "" 
       ' Call Read before accessing data. 
       While reader.Read() 
        'Get data 
        Id = reader(0) 
       End While 
       'Close Reader 
       reader.Close() 
       If Id <> "" Then 
        'User found 
        userFound = True 
        'Prepare the second sql 
        Dim command2 As New OleDbCommand("SELECT [Password] FROM [Password] WHERE [Password] = '" & txtPassword.Text & "';", con) 
        'Prepare second reader 
        Dim reader2 As OleDbDataReader = command.ExecuteReader() 
        Dim Pass As String = "" 
        ' Call Read before accessing data. 
        While reader2.Read() 
         'Get tdata 
         Pass = reader2(0) 
        End While 
        reader.Close() 
        If Pass <> "" Then 
         'Pass found 
         passwordFound = True 
        Else 
         passwordFound = False 
        End If 
       Else 
        userFound = False 
       End If 
       'Close connection 
       con.Close() 
       'Clear connection pool 
       SqlConnection.ClearPool(con) 
      End Using 
      '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 
+0

アイデアのおかげで。私はセキュリティの問題を理解していますが、このアプリはプレゼンテーションとしてラップトップで実行され、野生では機能しません。私は初心者であり、これは私のための学習プロジェクトなので、SQLへの切り替えは私にとっては多少問題でした。私のDBはあなたが言った通りです。 EMAIL - EID - emailaddy。パスパスEID。これは違いがありますか? Ole接続はどのように使用できますか? – user7662393

+0

シンプルなgoogleのように、@ user7662393のリサーチをお勧めします。 - 「Ole接続はどのように使用できますか?」それでも関連するものが出てくるでしょう –

+0

私はオレの基本的な理解があります。私はこのDBの4つのテーブルに接続して読み込み、コンボ/リストボックスを作成します。それで、私は専門家ではないが、私のDBに接続して読むことができる。 @Danny Jamesを検証するために2つのテーブルから読み込むという問題を解決できません。 – user7662393

0

1つのテーブルに電子メールとパスワードを組み合わせることにしました。今はすべてを簡単にしました。あなたの助けと提案に感謝します。

関連する問題