2017-04-24 8 views
0

私は、ユーザーがtxtUsernameとtxtPassに資格情報を入力するログインフォームを持っています。ユーザーと管理者が登録されているテーブルから自動的に選択します。ユーザーがテーブルのUSER-TYPEに応じて資格情報を入力したときに、指定されたフォーム(adminform)と(userform)が表示されます。私はデータベースとブール値にSQL SERVER 2012を使用しました。私はこれでブール値が必要ですか?私はブール値を設定し、どのようにそれを真実にしますか?.NETのユーザータイプに応じて、指定されたフォーム(adminformとuserform)を自動的に表示する方法は?

メトロメッセージボックスを無視します。メトロフレームワークだけです。

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click 
    Dim con As New SqlConnection 
    Dim cmd As New SqlCommand 
    Dim dr As SqlDataReader 
    Dim choice As Boolean 
    con.ConnectionString = My.Settings.ConString 
    cmd.Connection = con 
    If txtUser.Text = "" Or txtPass.Text = "" Then 
     MetroFramework.MetroMessageBox.Show(Me, "Invalid Login", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     txtUser.Clear() 
     txtPass.Clear() 
     txtUser.Focus() 
    ElseIf choice = True Then 
     Try 
      con.Open() 
      cmd.CommandText = "SELECT Name, Password from UserTable where Name = '" & txtUser.Text & "' and Password = '" & txtPass.Text & "' and [User Type]='User'" 

      dr = cmd.ExecuteReader 
      If dr.HasRows Then 
       UserMain.Show() 
       UserMain.lblName.Text = txtUser.Text 
       Me.Hide() 
      End If 
     Finally 
      con.Close() 
     End Try 
    ElseIf choice = False Then 
     Try 
      con.Open() 
      cmd.CommandText = "SELECT Name, Password from UserTable where Name = '" & txtUser.Text & "' and Password = '" & txtPass.Text & "' and [User Type]='Admin'" 

      dr = cmd.ExecuteReader 
      If dr.HasRows Then 
       AdminMain.Show() 
       Me.Hide() 
      End If 
     Finally 
      con.Close() 
     End Try 
    Else 
     MetroFramework.MetroMessageBox.Show(Me, "Invalid Login", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End If 
End Sub 
+1

あなたの質問を指定してください – Prisoner

+0

私はすでに私の質問を変更しました –

+0

コードを見れば、2つの脆弱性が明らかです。あなたは[SQLインジェクション](https://en.wikipedia.org/wiki/SQL_injection)につながるクエリーにユーザー入力を連結しており、ユーザーが入力したパスワードとDBを直接比較しているので、それを平文で保存します。少なくとも[PBKDF2](https://en.wikipedia.org/wiki/PBKDF2)を使用してください。 – Alejandro

答えて

0

@Alejandroで述べたように、SQLインジェクションが発生しやすく、パスワードをプレーンテキストとして保存するのは悪い習慣です。しかし、あなたの質問に答えるには、[User Type]の戻り値を確認することができます。

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click 
Dim con As New SqlConnection 
Dim cmd As New SqlCommand 
Dim dr As SqlDataReader 
Dim choice As Boolean 
con.ConnectionString = My.Settings.ConString 
cmd.Connection = con 
If txtUser.Text = "" Or txtPass.Text = "" Then 
    MetroFramework.MetroMessageBox.Show(Me, "Invalid Login", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    txtUser.Clear() 
    txtPass.Clear() 
    txtUser.Focus() 
Else 
    Try 
     con.Open() 
     cmd.CommandText = "SELECT Name, Password, [User Type] from UserTable where Name = '" & txtUser.Text & "' and Password = '" & txtPass.Text & "'" 

     dr = cmd.ExecuteReader 
     If dr.HasRows Then 
    if dr.item("User Type") = "Admin" Then 
      AdminMain.Show() 
       Me.Hide() 
    Else 
     UserMain.Show() 
       UserMain.lblName.Text = txtUser.Text 
       Me.Hide() 
    End If 
    Else 
    MetroFramework.MetroMessageBox.Show(Me, "Invalid Login", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     End If 
Catch myerror As SqlException 
    MessageBox.Show("An error Occurred: " & myerror.Message) 
    Finally 
     con.Close() 
    End Try 
End Sub 

もMetroframeworkをインポートすると、MetroFrameworkワード

MetroMessageBox.Show(Me, "Invalid Login", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error) 

を使用して削除されますが、この情報がお役に立てば幸い!

関連する問題