Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.OleDb
- Imports System.Net.Mail
- Imports System.Security.Cryptography
- Imports System.Text
- Public Class Form1
- Dim DatabaseConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Distribution.accdb")
- Dim name As String
- Dim userid As Integer
- Dim admin As Boolean = True
- Dim user As Boolean
- Dim Logtime As DateTime
- Dim endtime As DateTime
- Dim duration As TimeSpan
- Dim login As Boolean = False
- Dim loginadmin As Boolean = False
- Dim passwordemail As String
- Dim usernameemail As String
- Dim nameemail As String
- Dim pword As String
- Dim email As String
- Dim pwordattempts As Integer = 3
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnViewInventory.Click
- Inventory.Show()
- Me.Close()
- user = False
- admin = False
- End Sub
- Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
- If userid = 0 Then
- Me.Close()
- Else
- dbCon()
- Dim query2 As String = "INSERT INTO loggedUsers (DateAndTime,UserID,Logged) VALUES (@Date, @UserID, @Logged)"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query2
- .Parameters.AddWithValue("@Date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
- .Parameters.AddWithValue("@UserID", userid)
- .Parameters.AddWithValue("@Logged", "Logged Out")
- End With
- cmd.ExecuteNonQuery()
- userid = Nothing
- End Using
- MsgBox("Logged Out Before closing")
- DatabaseConnection.Close()
- Me.Close()
- End If
- End Sub
- Private Sub btnViewWarehouse_Click(sender As Object, e As EventArgs) Handles btnViewWarehouse.Click
- Nodes.Show()
- Me.Close()
- End Sub
- Private Sub btnViewTrucks_Click(sender As Object, e As EventArgs) Handles btnViewTrucks.Click
- Truck_screen.Show()
- Me.Close()
- End Sub
- Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
- AddToDatabase.Show()
- Me.Close()
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- btnViewInventory.Visible = False
- btnQuit.Visible = True
- btnViewTrucks.Visible = False
- btnViewWarehouse.Visible = False
- btnLog.Visible = True
- btnForgot.Visible = True
- btnLogin.Visible = True
- btnUsers.Visible = False
- btnAdmin.Visible = False
- Button1.Visible = False
- txtuserName.Visible = True
- txtpassWord.Visible = True
- btnLogout.Visible = False
- BtnSign.Visible = True
- txtEmail.Visible = True
- Label1.Visible = True
- End Sub
- Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
- txtuserName.Visible = True
- txtpassWord.Visible = True
- btnLog.Visible = True
- user = True
- End Sub
- Function hashCheck(pw As String) As String
- Using hash As MD5 = MD5.Create
- Dim byteArray As Byte() = hash.ComputeHash(Encoding.UTF8.GetBytes(pw))
- Dim stringbuilder As New StringBuilder
- For i = 0 To byteArray.Length - 1
- stringbuilder.Append(byteArray(i).ToString("X2"))
- Next
- Return stringbuilder.ToString
- End Using
- End Function
- Sub checkUsernamePassworduser()
- DatabaseConnection.Open()
- Dim saltedHash As String
- Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Alias", txtuserName.Text)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- read.Read()
- saltedHash = read.Item("Password").ToString
- name = read.Item("NameOfUser").ToString
- End Using
- End Using
- Dim split = saltedHash.Split(":"c)
- Dim salt = split(0)
- Dim hash = split(1)
- If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
- allowuseraccess()
- MsgBox("Hello " & name)
- Else
- pwordattempts = pwordattempts - 1
- MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
- If pwordattempts = 0 Then
- Me.Close()
- End If
- End If
- DatabaseConnection.Close()
- End Sub
- Sub checkpasswordadmin()
- If txtpassWord.Text = Nothing And txtuserName.Text = Nothing Then
- MsgBox("please give a username and password")
- Else
- If txtpassWord.Text = Nothing Then
- MsgBox("Please Give an Password")
- Else
- If txtuserName.Text = Nothing Then
- MsgBox("Please give a username")
- Else
- DatabaseConnection.Open()
- Dim saltedHash As String
- Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias AND Login.Admin = -1"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Alias", txtuserName.Text)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- read.Read()
- name = read.Item("NameOfUser").ToString
- saltedHash = read.Item("Password")
- End Using
- End Using
- Dim split = saltedHash.Split(":"c)
- Dim salt = split(0)
- Dim hash = split(1)
- If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
- allowadminaccess()
- MsgBox("Welcome, " & name)
- Else
- pwordattempts = pwordattempts - 1
- MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
- If pwordattempts = 0 Then
- Me.Close()
- End If
- End If
- DatabaseConnection.Close()
- End If
- End If
- End If
- If txtpassWord.Text = Nothing Then
- MsgBox("Please Give an Password")
- Else
- If txtuserName.Text = Nothing Then
- MsgBox("Please give a username")
- Else
- DatabaseConnection.Open()
- Dim saltedHash As String
- Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias AND Login.Admin = -1"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Alias", txtuserName.Text)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- read.Read()
- name = read.Item("NameOfUser").ToString
- saltedHash = read.Item("Password")
- End Using
- End Using
- Dim split = saltedHash.Split(":"c)
- Dim salt = split(0)
- Dim hash = split(1)
- If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
- allowadminaccess()
- MsgBox("Welcome, " & name)
- Else
- pwordattempts = pwordattempts - 1
- MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
- If pwordattempts = 0 Then
- Me.Close()
- End If
- End If
- DatabaseConnection.Close()
- End If
- End If
- End Sub
- Sub allowuseraccess()
- btnViewInventory.Visible = True
- btnQuit.Visible = True
- btnViewTrucks.Visible = True
- btnViewWarehouse.Visible = True
- btnLog.Visible = False
- btnForgot.Visible = False
- btnLogin.Visible = False
- btnUsers.Visible = False
- btnAdmin.Visible = False
- txtuserName.Visible = False
- txtpassWord.Visible = False
- btnLogout.Visible = True
- BtnSign.Visible = False
- txtEmail.Visible = False
- Label1.Visible = False
- End Sub
- Sub allowadminaccess()
- btnViewInventory.Visible = True
- btnQuit.Visible = True
- btnViewTrucks.Visible = True
- btnViewWarehouse.Visible = True
- btnLog.Visible = False
- btnForgot.Visible = False
- btnLogin.Visible = False
- btnUsers.Visible = True
- btnAdmin.Visible = True
- Button1.Visible = True
- txtuserName.Visible = False
- txtpassWord.Visible = False
- btnLogout.Visible = True
- BtnSign.Visible = False
- txtEmail.Visible = False
- Label1.Visible = False
- End Sub
- Private Sub btnLog_Click(sender As Object, e As EventArgs) Handles btnLog.Click
- If admin = True Then
- checkpasswordadmin()
- Else
- checkUsernamePassworduser()
- End If
- Button1.Enabled = True
- End Sub
- Private Sub BtnSign_Click(sender As Object, e As EventArgs) Handles BtnSign.Click
- txtuserName.Visible = True
- txtpassWord.Visible = True
- btnLog.Visible = True
- admin = True
- End Sub
- Private Sub btnLogout_Click(sender As Object, e As EventArgs) Handles btnLogout.Click
- DatabaseConnection.Open()
- MsgBox("Logged Out User:" & " " & name)
- name = ""
- Button1.Enabled = False
- btnViewInventory.Enabled = False
- btnViewWarehouse.Enabled = False
- btnViewTrucks.Enabled = False
- txtuserName.Visible = False
- txtpassWord.Visible = False
- btnLog.Visible = False
- btnLogout.Visible = False
- btnLogin.Visible = True
- BtnSign.Visible = True
- btnAdmin.Visible = False
- btnUsers.Visible = False
- endtime = DateTime.Now
- Dim diff As TimeSpan = DateTime.Parse(endtime).Subtract(DateTime.Parse(Logtime))
- Dim query2 As String = "INSERT INTO loggedUsers (DateAndTime,UserID,Logged,Duration) VALUES (@Date, @UserID, @Logged, @Duration)"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query2
- .Parameters.AddWithValue("@Date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
- .Parameters.AddWithValue("@UserID", userid)
- .Parameters.AddWithValue("@Logged", "Logged Out")
- .Parameters.AddWithValue("@Duration", diff.TotalSeconds)
- End With
- cmd.ExecuteNonQuery()
- userid = Nothing
- login = False
- End Using
- DatabaseConnection.Close()
- End Sub
- Private Sub btnAdmin_Click(sender As Object, e As EventArgs) Handles btnAdmin.Click
- New_User.Show()
- Me.Close()
- End Sub
- Private Sub btnUsers_Click(sender As Object, e As EventArgs) Handles btnUsers.Click
- User_Information_table.Show()
- Me.Close()
- End Sub
- Sub dbCon()
- If DatabaseConnection.State = ConnectionState.Open Then
- Else
- DatabaseConnection.Open()
- End If
- End Sub
- Sub findEmail(ByVal emailvar As String)
- dbCon()
- Dim query As String = "SELECT Login.Email FROM Login WHERE Login.Email=@email"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@email", emailvar)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- If read.HasRows Then
- Else
- Try
- Catch ex As Exception
- MsgBox("Email Does Not Exist in the Database")
- End Try
- End If
- End Using
- End Using
- DatabaseConnection.Close()
- End Sub
- Sub finddetails()
- dbCon()
- Dim query As String = "SELECT Login.Username, Login.NameOfUser FROM Login WHERE Login.Email=@Email"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Email", txtEmail.Text)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- If read.HasRows Then
- nameemail = read.Item("NameOfUser").ToString
- usernameemail = read.Item("Username")
- End If
- End Using
- End Using
- End Sub
- Sub CheckEmail()
- dbCon()
- Dim query As String = "SELECT Login.Email FROM Login WHERE Login.Email=@Email"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Email", txtEmail.Text)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- If read.HasRows Then
- Else
- Try
- Catch ex As Exception
- MsgBox("No Email exists")
- End Try
- End If
- End Using
- End Using
- End Sub
- Function findpassword(ByRef emailvar As String)
- dbCon()
- Dim passwordint(20) As Integer
- Dim passwordchar(20) As Char
- Dim query As String = "SELECT Login.[Password] FROM Login WHERE Login.Email=@Email"
- Using cmd As OleDbCommand = New OleDbCommand
- With cmd
- .Connection = DatabaseConnection
- .CommandType = CommandType.Text
- .CommandText = query
- .Parameters.AddWithValue("@Email", emailvar)
- End With
- Using read As OleDbDataReader = cmd.ExecuteReader
- If read.HasRows Then
- While read.Read
- passwordemail = read.Item("[Password]").ToString
- End While
- Else
- Try
- Catch ex As Exception
- MsgBox("No Password exists witht hat emailk")
- End Try
- End If
- End Using
- End Using
- DatabaseConnection.Close()
- For i = 0 To passwordemail.Length - 1
- passwordint(i) = Asc(txtpassWord.Text.Chars(i)) - 5
- passwordchar(i) = Chr(passwordint(i))
- pword = pword + passwordchar(i)
- Next
- Return pword
- DatabaseConnection.Close()
- End Function
- Private Sub btnForgot_Click(sender As Object, e As EventArgs) Handles btnForgot.Click
- Try
- CheckEmail()
- finddetails()
- findpassword(txtEmail.Text)
- Dim Smtp_Server As New SmtpClient
- Dim e_mail As New MailMessage()
- Smtp_Server.UseDefaultCredentials = False
- Smtp_Server.Credentials = New Net.NetworkCredential("sam.heath1998@gmail.com", "Edwards1970")
- Smtp_Server.Port = 587
- Smtp_Server.EnableSsl = True
- Smtp_Server.Host = "smtp.gmail.com"
- Smtp_Server.EnableSsl = True
- e_mail = New MailMessage()
- e_mail.From = New MailAddress("sam.heath1998@gmail.com")
- e_mail.To.Add(txtEmail.Text)
- e_mail.Subject = "ASBA Health and Welfare Forgotten Password"
- e_mail.IsBodyHtml = False
- e_mail.Body = "Heyy " & nameemail & "," & vbCrLf & "Here's your username and password" & vbCrLf & vbCrLf & "Username: " & usernameemail & vbCrLf & "Password: " & pword & vbCrLf & vbCrLf & "Please Contact an Administrator is problems persist"
- Smtp_Server.Send(e_mail)
- MsgBox("Username and Password Sent to " & nameemail)
- Catch error_t As Exception
- MsgBox(error_t.ToString)
- End Try
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement