Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET][MySQL]Login + Register Class


Some people askes, if there's a register system belonging to some Login Systems, so I wrote a little Class which allows you to do that.


Current Solutions For Error's

Ok, if u have some error's with (anytype of this) " MySql.Data.MySqlClient"
Then download the MySql.Data.dll(At Requirements) OR [ Download ]
Then add it into your project : Project > Add Existing Item > Filter so you can select a .DLL > Navigate to MySql.Data.dll > Add It , Now the most errors should be gone!
Got still error's?
Download the source below the thread.(U can mod it if u want.)


Introducing

This is a VB.NET Class, which allows your users to register an account, which gets saved on a MySQL Database online, so they can login everywhere.
It includes MD5 hashing of your password and usernames to increase the securtity.


Requirements
  • VB.NET Ide
  • Basic Knowledge of VB.NET
  • MySQL DLL for VB [ Download ] [ Virusscan ]
  • A MySQL Database, which allows external acess ( Ohost/Funpic don't ) ( Just google or use something like www.db4free.net )

Getting Started


At first you need to create the needed databases. Use this as SQL command. Go to your PHPMyAdmin, select your database and click on SQL. ( Look here )

Add this:

Code
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `ID` int(5) NOT NULL auto_increment,
  `username` varchar(255) default NULL,
  `password` varchar(255) default NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
press OK and submit.

Then add the MD5 Module to your project: ( Credits go to: http://dotnet-snippets.de )

Code
Imports System.Security.Cryptography
Imports System.Text
Module MD5SECURITY
    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 Module

Then the Registering class RegSys.vb:

Code
Imports MySql.Data.MySqlClient
Public Class RegSys
    Private _connection As MySqlConnection = New MySqlConnection
    Private _adapter As New MySqlDataAdapter
    Private _query As String
    Private _command As New MySqlCommand
    Private _user As String
    Private _password(2) As String
    Public _continue As Boolean = False

    Public Function SetConnectionData(ByVal ip As String, ByVal username As String, ByVal pw As String, ByVal database As String)
  _connection.ConnectionString = "server=" & ip & ";" _
  & "user id=" & username & ";" _
  & "password=" & pw & ";" _
  & "database=" & database
    End Function

    Public Function InitializeLogin(ByVal usernametextbox As TextBox, ByVal pwtextbox As TextBox)
  _user = MD5StringHash(usernametextbox.Text)
  _password(0) = MD5StringHash(pwtextbox.Text)
  Try
    _connection.Open()    ' Try to connect
  Catch myerror As MySqlException
    MsgBox("No Connection to the Database!", MsgBoxStyle.Critical, "Fatal Error!")  ' If the person fails to connect
  End Try
  _query = "SELECT * FROM users WHERE username ='" + Replace(_user, " ", "") + "' AND password='" & Replace(_password(0), " ", "") & "'"
  _command.Connection = _connection
  _command.CommandText = _query

  _adapter.SelectCommand = _command
  Dim _myData As MySqlDataReader
  _myData = _command.ExecuteReader()    ' Start of the query
  If _myData.HasRows Then  ' If the query contains rows ( the password and username were right )
    MsgBox("Logon sucessful!")
    _connection.Close()
    _continue = True
  Else    ' If username / password are wrong
    MsgBox("Error! Wrong username/password!")
    _continue = False
  End If
    End Function

    Public Function InitializeRegister(ByVal usernametextbox As TextBox, ByVal pw1textbox As TextBox, ByVal pw2textbox As TextBox)
  _user = MD5StringHash(usernametextbox.Text)
  _password(0) = MD5StringHash(pw1textbox.Text)
  _password(1) = MD5StringHash(pw2textbox.Text)
  If _password(0) = _password(1) Then  ' If the textboxes passwords are the same
    Try
    _connection.Open()    ' Open the connection
    Catch myerror As MySqlException
    MsgBox("No connection to the database!", MsgBoxStyle.Critical, "Fatal Error!")
    End Try

    Dim myAdapter As New MySqlDataAdapter
    Dim SQLAbfrage As String = "SELECT * FROM users WHERE username='" + _user + "'" ' Query if the user already exists
    Dim _tmp_myCommand As New MySqlCommand
    _tmp_myCommand.Connection = _connection
    _tmp_myCommand.CommandText = SQLAbfrage

    myAdapter.SelectCommand = _tmp_myCommand
    Dim myData As MySqlDataReader
    myData = _tmp_myCommand.ExecuteReader()  ' Start query

    If myData.HasRows = 0 Then    ' If the username already exists it won't begin with the registration
    _connection.Close()
    _connection.Open()
    Dim registerfinal As New MySqlDataAdapter
    _tmp_myCommand.CommandText = "INSERT INTO users(username, password)" _
  & "VALUES('" & _user & "','" & _password(0) & "')"
    _tmp_myCommand.ExecuteNonQuery()    ' Start SQL query and insert
    MsgBox("The account with the name: " & usernametextbox.Text & " was created sucessfully!", MsgBoxStyle.Information, "Yep =)")
    _connection.Close()
    Else
    MsgBox("This username does already exist", MsgBoxStyle.Information, "Sorry")
    End If
  Else
    MsgBox("The passwords did not match!", MsgBoxStyle.Critical, "Error!")
  End If
    End Function
End Class

The rest is quite easy, you just need to set the connection from the class and reference the textboxes.

The register Form should look like this:
[img]http://www.abload.de/img/scrn_reg_form7lr2.png[/img]

The code for registering is:
Code
Dim myRegSys As New RegSys
myRegSys.SetConnectionData("12.345.678.910", "username", "password", "database")
myRegSys.InitializeRegister(TextBox1, TextBox2, TextBox3)

Put this into the Button Sub! On SetConnectionData replace 12.345.678.910 with your Server ip! Username and password are replaced with your database username and password and database is the database where you created the table.
Also replace the textboxes with the names of yours. (TextBox1 = username, TextBox2 = password, TextBox3 = Retyped password)

The Login Form should look like this:
[img]http://www.abload.de/img/scrn_login_form9b5m.png[/img]

The code for logging in is: ( Nearly the same )
Code
Dim myRegSys As New RegSys
myRegSys.SetConnectionData("12.345.678.910", "username", "password", "database")
myRegSys.InitializeLogin(TextBox1, TextBox2)

SetConnectionData will be the same like registering. Also the TextBoxes. (TextBox1 = username, TextBox2 = password)
If anything is unclear, look at the comments in the class or ask here :)

Download Source
http://www.mediafire.com/download.php?zvqynz4zo5r
FR1908
Thank you. I will try it
Can you give me download for source code ?
Lol it works thank you very much
Very nice tut man Thanks!
[Image: l8foq.png]
i have been looking for this thank you so much...
can you give the source code/project of this please.
can one tell me how can i fix this?
http://img7.imageshack.us/img7/7590/posting1i.jpg
Import into project Mysql.Data from .NET tab
i did that.!!!!!!!!!!!!!!
so ignore it, it does not mean anything, I got it too
do i have to edit this code?

Quote: Public Function SetConnectionData(ByVal ip As String, ByVal username As String, ByVal pw As String, ByVal database As String)
connection.ConnectionString = "server=" & ip & ";"
& "user id=" & username & ";"
& "password=" & pw & ";"

& "database=" & database
End Function
No there are just warnings, they wont infect your program.
FR1908
i think they did buc when i click on login or Register i get this error " no connection to the database! "
OMG , hey , The-One

i need help with this : i want to see in textbox5.text infromation from table in mysql it is possible ?
why am i getting this ever time i try to login or Register can some one please help me??!!??!!??
[img]http://img22.imageshack.us/img22/7470/posting1.jpg[/img]
I dont know sure, but i ALWAYS use database from db4free.net try using that database...
FR1908


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by optionalMay 21, 2021 08:42 AM 7 309 May 21, 2021 08:42 AM
Last Post: Charger
[C] by lockheadApr 22, 2021 07:10 AM 0 146 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C] by MeterpreterJun 27, 2018 09:06 PM 17 516 Jun 27, 2018 09:06 PM
Last Post: Meterpreter