Advertisement
Guest User

login

a guest
Dec 14th, 2018
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.47 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Imports System.Security.Cryptography
  3. Imports System.Text
  4. Public Class FrmLogin
  5.     Public connDB As New MySqlConnection
  6.     Public comDB As New MySqlCommand
  7.     Public rdDB As MySqlDataReader
  8.     Public myError As MySqlError
  9.     Public SQL As String
  10.     Sub conectDB()
  11.         Dim strServer As String = "localhost"
  12.         Dim strDbase As String = "testdb"
  13.         Dim strUser As String = "root"
  14.         Dim strPass As String = ""
  15.  
  16.         If connDB.State <> ConnectionState.Open Then connDB.ConnectionString = "server=" & strServer.Trim & ";database=" & strDbase.Trim & ";user=" & strUser.Trim & ";password=" & strPass
  17.         If connDB.State <> ConnectionState.Open Then connDB.Open()
  18.     End Sub
  19.     Sub closeDB()
  20.         If connDB.State <> ConnectionState.Closed Then connDB.Close()
  21.     End Sub
  22.     Sub initCMD()
  23.         With comDB
  24.             .Connection = connDB
  25.             .CommandType = CommandType.Text
  26.             .CommandTimeout = 280000
  27.         End With
  28.     End Sub
  29.  
  30.     Function getMD5Hash(ByVal strToHash As String) As String
  31.         Dim md5Obj As New MD5CryptoServiceProvider()
  32.         Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
  33.         bytesToHash = md5Obj.ComputeHash(bytesToHash)
  34.         Dim strResult As String = ""
  35.         Dim b As Byte
  36.         For Each b In bytesToHash
  37.             strResult += b.ToString("x2")
  38.         Next
  39.         Return strResult
  40.     End Function
  41.  
  42.     Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  43.         txtPassword.Text = ""
  44.         txtUsername.Text = ""
  45.         txtUsername.Select()
  46.     End Sub
  47.     Private Function ValidasiEntry() As Boolean
  48.         If Me.txtUsername.Text.Trim = "" Or
  49.             Me.txtPassword.Text.Trim = "" Then
  50.             MsgBox("Username & Password Tidak Boleh Kosong!", MsgBoxStyle.Exclamation, "Error")
  51.             Return True
  52.         End If
  53.     End Function
  54.     Private Sub CekUser()
  55.         Try
  56.             SQL = "SELECT * from users " &
  57.                  "WHERE status = '1' " &
  58.                  "AND username ='" & Me.txtUsername.Text & "' " &
  59.                  "AND password ='" & getMD5Hash(txtPassword.Text) & "'"
  60.  
  61.             With comDB
  62.                 .CommandText = SQL
  63.                 .ExecuteNonQuery()
  64.             End With
  65.             rdDB = comDB.ExecuteReader
  66.             rdDB.Read()
  67.             If rdDB.HasRows = True Then
  68.                 Me.Hide()
  69.                 Form1.Show()
  70.             Else
  71.                 MsgBox("Username and or Password not found!", MsgBoxStyle.Exclamation, "Information")
  72.                 txtUsername.Text = ""
  73.                 txtPassword.Text = ""
  74.                 txtUsername.Select()
  75.             End If
  76.             rdDB.Close()
  77.         Catch ex As Exception
  78.             MsgBox(ex.ToString)
  79.         End Try
  80.     End Sub
  81.     Private Sub LoadData(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtUsername.KeyDown, txtPassword.KeyDown
  82.         If e.KeyCode = Keys.Enter Then
  83.             ProcessTabKey(True)
  84.         End If
  85.     End Sub
  86.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  87.         If ValidasiEntry() = True Then
  88.             txtUsername.Select()
  89.             Exit Sub
  90.         End If
  91.  
  92.         Call conectDB()
  93.         Call initCMD()
  94.         Call CekUser()
  95.     End Sub
  96.  
  97.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  98.         Me.Close()
  99.     End Sub
  100. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement