Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Data.OleDb
  2. Imports System.Net.Mail
  3. Imports System.Security.Cryptography
  4. Imports System.Text
  5. Public Class Form1
  6.     Dim DatabaseConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Distribution.accdb")
  7.     Dim name As String
  8.     Dim userid As Integer
  9.     Dim admin As Boolean = True
  10.     Dim user As Boolean
  11.     Dim Logtime As DateTime
  12.     Dim endtime As DateTime
  13.     Dim duration As TimeSpan
  14.     Dim login As Boolean = False
  15.     Dim loginadmin As Boolean = False
  16.     Dim passwordemail As String
  17.     Dim usernameemail As String
  18.     Dim nameemail As String
  19.     Dim pword As String
  20.     Dim email As String
  21.     Dim pwordattempts As Integer = 3
  22.  
  23.  
  24.  
  25.  
  26.  
  27.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnViewInventory.Click
  28.         Inventory.Show()
  29.         Me.Close()
  30.         user = False
  31.         admin = False
  32.  
  33.  
  34.     End Sub
  35.  
  36.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
  37.  
  38.         If userid = 0 Then
  39.             Me.Close()
  40.         Else
  41.             dbCon()
  42.             Dim query2 As String = "INSERT INTO loggedUsers (DateAndTime,UserID,Logged) VALUES (@Date, @UserID, @Logged)"
  43.             Using cmd As OleDbCommand = New OleDbCommand
  44.                 With cmd
  45.                     .Connection = DatabaseConnection
  46.                     .CommandType = CommandType.Text
  47.                     .CommandText = query2
  48.                     .Parameters.AddWithValue("@Date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
  49.                     .Parameters.AddWithValue("@UserID", userid)
  50.                     .Parameters.AddWithValue("@Logged", "Logged Out")
  51.                 End With
  52.                 cmd.ExecuteNonQuery()
  53.                 userid = Nothing
  54.  
  55.             End Using
  56.             MsgBox("Logged Out Before closing")
  57.             DatabaseConnection.Close()
  58.             Me.Close()
  59.  
  60.  
  61.  
  62.         End If
  63.     End Sub
  64.  
  65.     Private Sub btnViewWarehouse_Click(sender As Object, e As EventArgs) Handles btnViewWarehouse.Click
  66.         Nodes.Show()
  67.         Me.Close()
  68.  
  69.     End Sub
  70.  
  71.     Private Sub btnViewTrucks_Click(sender As Object, e As EventArgs) Handles btnViewTrucks.Click
  72.         Truck_screen.Show()
  73.         Me.Close()
  74.  
  75.     End Sub
  76.  
  77.     Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
  78.         AddToDatabase.Show()
  79.         Me.Close()
  80.     End Sub
  81.  
  82.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  83.         btnViewInventory.Visible = False
  84.         btnQuit.Visible = True
  85.         btnViewTrucks.Visible = False
  86.         btnViewWarehouse.Visible = False
  87.         btnLog.Visible = True
  88.         btnForgot.Visible = True
  89.         btnLogin.Visible = True
  90.         btnUsers.Visible = False
  91.         btnAdmin.Visible = False
  92.         Button1.Visible = False
  93.         txtuserName.Visible = True
  94.         txtpassWord.Visible = True
  95.         btnLogout.Visible = False
  96.         BtnSign.Visible = True
  97.         txtEmail.Visible = True
  98.         Label1.Visible = True
  99.  
  100.  
  101.  
  102.     End Sub
  103.  
  104.     Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
  105.         txtuserName.Visible = True
  106.         txtpassWord.Visible = True
  107.         btnLog.Visible = True
  108.         user = True
  109.  
  110.     End Sub
  111.     Function hashCheck(pw As String) As String
  112.         Using hash As MD5 = MD5.Create
  113.             Dim byteArray As Byte() = hash.ComputeHash(Encoding.UTF8.GetBytes(pw))
  114.             Dim stringbuilder As New StringBuilder
  115.             For i = 0 To byteArray.Length - 1
  116.                 stringbuilder.Append(byteArray(i).ToString("X2"))
  117.             Next
  118.             Return stringbuilder.ToString
  119.         End Using
  120.  
  121.     End Function
  122.     Sub checkUsernamePassworduser()
  123.  
  124.         DatabaseConnection.Open()
  125.         Dim saltedHash As String
  126.         Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias"
  127.         Using cmd As OleDbCommand = New OleDbCommand
  128.             With cmd
  129.                 .Connection = DatabaseConnection
  130.                 .CommandType = CommandType.Text
  131.                 .CommandText = query
  132.                 .Parameters.AddWithValue("@Alias", txtuserName.Text)
  133.             End With
  134.  
  135.             Using read As OleDbDataReader = cmd.ExecuteReader
  136.                 read.Read()
  137.                 saltedHash = read.Item("Password").ToString
  138.                 name = read.Item("NameOfUser").ToString
  139.             End Using
  140.         End Using
  141.         Dim split = saltedHash.Split(":"c)
  142.         Dim salt = split(0)
  143.         Dim hash = split(1)
  144.         If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
  145.             allowuseraccess()
  146.             MsgBox("Hello " & name)
  147.         Else
  148.             pwordattempts = pwordattempts - 1
  149.  
  150.             MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
  151.             If pwordattempts = 0 Then
  152.                 Me.Close()
  153.             End If
  154.  
  155.         End If
  156.         DatabaseConnection.Close()
  157.  
  158.  
  159.     End Sub
  160.     Sub checkpasswordadmin()
  161.         If txtpassWord.Text = Nothing And txtuserName.Text = Nothing Then
  162.             MsgBox("please give a username and password")
  163.         Else
  164.             If txtpassWord.Text = Nothing Then
  165.                 MsgBox("Please Give an Password")
  166.             Else
  167.                 If txtuserName.Text = Nothing Then
  168.                     MsgBox("Please give a username")
  169.                 Else
  170.                     DatabaseConnection.Open()
  171.                     Dim saltedHash As String
  172.                     Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias AND Login.Admin = -1"
  173.                     Using cmd As OleDbCommand = New OleDbCommand
  174.                         With cmd
  175.                             .Connection = DatabaseConnection
  176.                             .CommandType = CommandType.Text
  177.                             .CommandText = query
  178.                             .Parameters.AddWithValue("@Alias", txtuserName.Text)
  179.                         End With
  180.  
  181.                         Using read As OleDbDataReader = cmd.ExecuteReader
  182.                             read.Read()
  183.                             name = read.Item("NameOfUser").ToString
  184.                             saltedHash = read.Item("Password")
  185.                         End Using
  186.                     End Using
  187.                     Dim split = saltedHash.Split(":"c)
  188.                     Dim salt = split(0)
  189.                     Dim hash = split(1)
  190.                     If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
  191.                         allowadminaccess()
  192.                         MsgBox("Welcome, " & name)
  193.                     Else
  194.                         pwordattempts = pwordattempts - 1
  195.  
  196.                         MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
  197.                         If pwordattempts = 0 Then
  198.                             Me.Close()
  199.                         End If
  200.                     End If
  201.  
  202.                     DatabaseConnection.Close()
  203.                 End If
  204.             End If
  205.         End If
  206.         If txtpassWord.Text = Nothing Then
  207.             MsgBox("Please Give an Password")
  208.         Else
  209.             If txtuserName.Text = Nothing Then
  210.                 MsgBox("Please give a username")
  211.             Else
  212.                 DatabaseConnection.Open()
  213.                 Dim saltedHash As String
  214.                 Dim query As String = "SELECT Login.Alias, Login.Password, Login.NameOfUser FROM Login WHERE Login.Alias = @Alias AND Login.Admin = -1"
  215.                 Using cmd As OleDbCommand = New OleDbCommand
  216.                     With cmd
  217.                         .Connection = DatabaseConnection
  218.                         .CommandType = CommandType.Text
  219.                         .CommandText = query
  220.                         .Parameters.AddWithValue("@Alias", txtuserName.Text)
  221.                     End With
  222.  
  223.                     Using read As OleDbDataReader = cmd.ExecuteReader
  224.                         read.Read()
  225.                         name = read.Item("NameOfUser").ToString
  226.                         saltedHash = read.Item("Password")
  227.                     End Using
  228.                 End Using
  229.                 Dim split = saltedHash.Split(":"c)
  230.                 Dim salt = split(0)
  231.                 Dim hash = split(1)
  232.                 If Asc(hashCheck(txtpassWord.Text)) = Asc(hash) Then
  233.                     allowadminaccess()
  234.                     MsgBox("Welcome, " & name)
  235.                 Else
  236.                     pwordattempts = pwordattempts - 1
  237.  
  238.                     MsgBox("Your Password Is incorrect, Attempts Left: " & pwordattempts)
  239.                     If pwordattempts = 0 Then
  240.                         Me.Close()
  241.                     End If
  242.                 End If
  243.  
  244.                 DatabaseConnection.Close()
  245.             End If
  246.         End If
  247.     End Sub
  248.  
  249.  
  250.     Sub allowuseraccess()
  251.         btnViewInventory.Visible = True
  252.         btnQuit.Visible = True
  253.         btnViewTrucks.Visible = True
  254.         btnViewWarehouse.Visible = True
  255.         btnLog.Visible = False
  256.         btnForgot.Visible = False
  257.         btnLogin.Visible = False
  258.         btnUsers.Visible = False
  259.         btnAdmin.Visible = False
  260.         txtuserName.Visible = False
  261.         txtpassWord.Visible = False
  262.         btnLogout.Visible = True
  263.         BtnSign.Visible = False
  264.         txtEmail.Visible = False
  265.         Label1.Visible = False
  266.  
  267.  
  268.  
  269.  
  270.     End Sub
  271.     Sub allowadminaccess()
  272.         btnViewInventory.Visible = True
  273.         btnQuit.Visible = True
  274.         btnViewTrucks.Visible = True
  275.         btnViewWarehouse.Visible = True
  276.         btnLog.Visible = False
  277.         btnForgot.Visible = False
  278.         btnLogin.Visible = False
  279.         btnUsers.Visible = True
  280.         btnAdmin.Visible = True
  281.         Button1.Visible = True
  282.         txtuserName.Visible = False
  283.         txtpassWord.Visible = False
  284.         btnLogout.Visible = True
  285.         BtnSign.Visible = False
  286.         txtEmail.Visible = False
  287.         Label1.Visible = False
  288.  
  289.  
  290.     End Sub
  291.  
  292.     Private Sub btnLog_Click(sender As Object, e As EventArgs) Handles btnLog.Click
  293.         If admin = True Then
  294.             checkpasswordadmin()
  295.         Else
  296.             checkUsernamePassworduser()
  297.  
  298.         End If
  299.  
  300.  
  301.         Button1.Enabled = True
  302.     End Sub
  303.  
  304.     Private Sub BtnSign_Click(sender As Object, e As EventArgs) Handles BtnSign.Click
  305.         txtuserName.Visible = True
  306.         txtpassWord.Visible = True
  307.         btnLog.Visible = True
  308.         admin = True
  309.  
  310.  
  311.     End Sub
  312.  
  313.     Private Sub btnLogout_Click(sender As Object, e As EventArgs) Handles btnLogout.Click
  314.         DatabaseConnection.Open()
  315.         MsgBox("Logged Out User:" & " " & name)
  316.         name = ""
  317.         Button1.Enabled = False
  318.         btnViewInventory.Enabled = False
  319.         btnViewWarehouse.Enabled = False
  320.         btnViewTrucks.Enabled = False
  321.         txtuserName.Visible = False
  322.         txtpassWord.Visible = False
  323.         btnLog.Visible = False
  324.         btnLogout.Visible = False
  325.         btnLogin.Visible = True
  326.         BtnSign.Visible = True
  327.         btnAdmin.Visible = False
  328.         btnUsers.Visible = False
  329.  
  330.         endtime = DateTime.Now
  331.         Dim diff As TimeSpan = DateTime.Parse(endtime).Subtract(DateTime.Parse(Logtime))
  332.  
  333.         Dim query2 As String = "INSERT INTO loggedUsers (DateAndTime,UserID,Logged,Duration) VALUES (@Date, @UserID, @Logged, @Duration)"
  334.         Using cmd As OleDbCommand = New OleDbCommand
  335.             With cmd
  336.                 .Connection = DatabaseConnection
  337.                 .CommandType = CommandType.Text
  338.                 .CommandText = query2
  339.                 .Parameters.AddWithValue("@Date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
  340.                 .Parameters.AddWithValue("@UserID", userid)
  341.                 .Parameters.AddWithValue("@Logged", "Logged Out")
  342.                 .Parameters.AddWithValue("@Duration", diff.TotalSeconds)
  343.             End With
  344.             cmd.ExecuteNonQuery()
  345.             userid = Nothing
  346.             login = False
  347.  
  348.  
  349.  
  350.         End Using
  351.         DatabaseConnection.Close()
  352.  
  353.  
  354.     End Sub
  355.  
  356.     Private Sub btnAdmin_Click(sender As Object, e As EventArgs) Handles btnAdmin.Click
  357.         New_User.Show()
  358.         Me.Close()
  359.     End Sub
  360.  
  361.     Private Sub btnUsers_Click(sender As Object, e As EventArgs) Handles btnUsers.Click
  362.         User_Information_table.Show()
  363.         Me.Close()
  364.  
  365.     End Sub
  366.     Sub dbCon()
  367.         If DatabaseConnection.State = ConnectionState.Open Then
  368.         Else
  369.  
  370.             DatabaseConnection.Open()
  371.         End If
  372.     End Sub
  373.     Sub findEmail(ByVal emailvar As String)
  374.         dbCon()
  375.         Dim query As String = "SELECT Login.Email FROM Login WHERE Login.Email=@email"
  376.         Using cmd As OleDbCommand = New OleDbCommand
  377.             With cmd
  378.                 .Connection = DatabaseConnection
  379.                 .CommandType = CommandType.Text
  380.                 .CommandText = query
  381.                 .Parameters.AddWithValue("@email", emailvar)
  382.             End With
  383.             Using read As OleDbDataReader = cmd.ExecuteReader
  384.                 If read.HasRows Then
  385.                 Else
  386.                     Try
  387.                     Catch ex As Exception
  388.                         MsgBox("Email Does Not Exist in the Database")
  389.                     End Try
  390.  
  391.                 End If
  392.  
  393.             End Using
  394.  
  395.         End Using
  396.  
  397.  
  398.         DatabaseConnection.Close()
  399.     End Sub
  400.     Sub finddetails()
  401.         dbCon()
  402.         Dim query As String = "SELECT Login.Username, Login.NameOfUser FROM Login WHERE Login.Email=@Email"
  403.         Using cmd As OleDbCommand = New OleDbCommand
  404.             With cmd
  405.                 .Connection = DatabaseConnection
  406.                 .CommandType = CommandType.Text
  407.                 .CommandText = query
  408.                 .Parameters.AddWithValue("@Email", txtEmail.Text)
  409.             End With
  410.             Using read As OleDbDataReader = cmd.ExecuteReader
  411.                 If read.HasRows Then
  412.                     nameemail = read.Item("NameOfUser").ToString
  413.                     usernameemail = read.Item("Username")
  414.  
  415.                 End If
  416.  
  417.             End Using
  418.         End Using
  419.     End Sub
  420.     Sub CheckEmail()
  421.         dbCon()
  422.         Dim query As String = "SELECT Login.Email FROM Login WHERE Login.Email=@Email"
  423.         Using cmd As OleDbCommand = New OleDbCommand
  424.             With cmd
  425.                 .Connection = DatabaseConnection
  426.                 .CommandType = CommandType.Text
  427.                 .CommandText = query
  428.                 .Parameters.AddWithValue("@Email", txtEmail.Text)
  429.             End With
  430.             Using read As OleDbDataReader = cmd.ExecuteReader
  431.                 If read.HasRows Then
  432.                 Else
  433.                     Try
  434.                     Catch ex As Exception
  435.                         MsgBox("No Email exists")
  436.                     End Try
  437.                 End If
  438.             End Using
  439.         End Using
  440.     End Sub
  441.     Function findpassword(ByRef emailvar As String)
  442.         dbCon()
  443.         Dim passwordint(20) As Integer
  444.         Dim passwordchar(20) As Char
  445.  
  446.  
  447.         Dim query As String = "SELECT Login.[Password] FROM Login WHERE Login.Email=@Email"
  448.         Using cmd As OleDbCommand = New OleDbCommand
  449.             With cmd
  450.                 .Connection = DatabaseConnection
  451.                 .CommandType = CommandType.Text
  452.                 .CommandText = query
  453.                 .Parameters.AddWithValue("@Email", emailvar)
  454.             End With
  455.             Using read As OleDbDataReader = cmd.ExecuteReader
  456.                 If read.HasRows Then
  457.                     While read.Read
  458.                         passwordemail = read.Item("[Password]").ToString
  459.                     End While
  460.  
  461.                 Else
  462.                     Try
  463.  
  464.                     Catch ex As Exception
  465.                         MsgBox("No Password exists witht hat emailk")
  466.                     End Try
  467.                 End If
  468.             End Using
  469.         End Using
  470.         DatabaseConnection.Close()
  471.         For i = 0 To passwordemail.Length - 1
  472.             passwordint(i) = Asc(txtpassWord.Text.Chars(i)) - 5
  473.             passwordchar(i) = Chr(passwordint(i))
  474.             pword = pword + passwordchar(i)
  475.         Next
  476.         Return pword
  477.         DatabaseConnection.Close()
  478.     End Function
  479.  
  480.     Private Sub btnForgot_Click(sender As Object, e As EventArgs) Handles btnForgot.Click
  481.         Try
  482.             CheckEmail()
  483.             finddetails()
  484.             findpassword(txtEmail.Text)
  485.  
  486.             Dim Smtp_Server As New SmtpClient
  487.             Dim e_mail As New MailMessage()
  488.             Smtp_Server.UseDefaultCredentials = False
  489.             Smtp_Server.Credentials = New Net.NetworkCredential("sam.heath1998@gmail.com", "Edwards1970")
  490.             Smtp_Server.Port = 587
  491.             Smtp_Server.EnableSsl = True
  492.             Smtp_Server.Host = "smtp.gmail.com"
  493.             Smtp_Server.EnableSsl = True
  494.             e_mail = New MailMessage()
  495.             e_mail.From = New MailAddress("sam.heath1998@gmail.com")
  496.             e_mail.To.Add(txtEmail.Text)
  497.             e_mail.Subject = "ASBA Health and Welfare Forgotten Password"
  498.             e_mail.IsBodyHtml = False
  499.             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"
  500.             Smtp_Server.Send(e_mail)
  501.             MsgBox("Username and Password Sent to " & nameemail)
  502.  
  503.         Catch error_t As Exception
  504.             MsgBox(error_t.ToString)
  505.         End Try
  506.  
  507.     End Sub
  508. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement