[help]MySql login system [help]

Posts 1–3 of 3 · Page 1 of 1
[php]'Coded by Blubb1337 from MPGH

Imports MySql.Data.MySqlClient
Imports System.Text
Imports System.Security.Cryptography

Public Class frmLogin

'make sure your database allows external access, else it will not work
'free databases mostly do not allow external access, there are a few which do though
'the only thing you need to fill in is obviously the server, database username/password/table and the name of the database itself

Private table As String = "User" 'name of the table
Private conn As New MySqlConnection("server=;" & "user id=;" & "password=;" & "database=")
Private reader As MySqlDataReader

'current database structure:
'create table `DBName`.`TableName`(
'`Index` int NOT NULL AUTO_INCREMENT ,
'`Username` mediumtext ,
'`Password` mediumtext ,
'`Email` mediumtext ,
' PRIMARY KEY (`Index`)
')

#Region "Functions"

'the md5 function crypts the password
'therefore it cannot be uncrypted again

Public Function MD5StringHash(ByVal strString As String) As String
Dim MD5 As New MD5CryptoServiceProvider
Dim Data As Byte()
Dim Result As Byte()
Dim Res As String = ""
Dim Tmp As String = ""

Data = Encoding.ASCII.GetBytes(strString)
Result = MD5.ComputeHash(Data)
For i As Integer = 0 To Result.Length - 1
Tmp = Hex(Result(i))
If Len(Tmp) = 1 Then Tmp = "0" & Tmp
Res += Tmp
Next
Return Res
End Function

#End Region

#Region "Database"

Private Sub Login()
cmdLogin.Enabled = False
cmdRegister.Enabled = False

Try
conn.Open()

Dim strSQL As String = "SELECT * FROM " & table & " WHERE Username='" & txtID.Text & "' AND Password='" & MD5StringHash(txtPW.Text) & "'"

Dim cmd As New MySqlCommand(strSQL, conn)
reader = cmd.ExecuteReader

If CInt(reader.HasRows) <> 0 Then
Me.Close()
frmMain.ShowDialog()
Else
MsgBox("Access denied.", MsgBoxStyle.Information)
End If

reader.Close()

conn.Close()
Catch ex As MySqlException
conn.Close()
End Try

cmdRegister.Enabled = True
cmdLogin.Enabled = True
End Sub

Private Sub Register()
cmdRegister.Enabled = False

Try
conn.Open()

Dim strSQL As String = "INSERT INTO " & table & "(Username,Password,Email) VALUES ('" & txtRegID.Text & "','" & MD5StringHash(txtRegPW.Text) & "','" & txtRegEmail.Text & "')"
Dim strSQL2 As String = "SELECT * FROM " & table & " WHERE Username='" & txtRegID.Text & "' OR Email='" & txtRegEmail.Text & "'"

Dim cmd As New MySqlCommand(strSQL, conn)
Dim cmd2 As New MySqlCommand(strSQL2, conn)

reader = cmd2.ExecuteReader

If CInt(reader.HasRows) = 0 Then
reader.Close()
cmd.ExecuteNonQuery()

MsgBox("Successfully created the account " & txtRegID.Text & ".", MsgBoxStyle.Information)
Else
reader.Close()
MsgBox("The username or email already exists.", MsgBoxStyle.Information)
End If

conn.Close()
Catch ex As MySqlException
conn.Close()
End Try

cmdRegister.Enabled = True
End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub

Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
Dim t As New Threading.Thread(AddressOf Login)
t.Start()
End Sub

Private Sub txtID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtID.TextChanged
My.Settings.id = txtID.Text
My.Settings.Save()
My.Settings.Reload()
End Sub

Private Sub txtPW_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPW.TextChanged
My.Settings.pw = txtPW.Text
My.Settings.Save()
My.Settings.Reload()
End Sub

Private Sub cmdRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegister.Click
Dim t As New Threading.Thread(AddressOf Register)
t.Start()
End Sub
End Class [/php]

Download the full project
thanks bubb i will reply if i have any more questions
Posts 1–3 of 3 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us