Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  1. Imports ADOX
  2. Imports System.IO
  3. Imports System.Data.OleDb
  4.  
  5.  
  6. Public Class Login
  7. Public Club As String
  8. Public AdminAccess As Boolean
  9. Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=N:\Whizzkids\WhizzkidsDB.accdb;"
  10.  
  11. Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12. 'This sub creates the file and all of the tables when the program is first run. It also run time generates the whole login screen.
  13.  
  14. Dim cat As Catalog = New Catalog()
  15. Dim SQLCommand As String
  16. Dim con As New OleDbConnection(ConnectionString)
  17. Dim cmd As New OleDbCommand
  18. Dim HashedAdminPWord As String = HashPassword("Password123")
  19.  
  20.  
  21. 'This checks if the file already exists
  22. If File.Exists("N:\Whizzkids\WhizzkidsDB.accdb") = True Then
  23. Else
  24. cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _
  25. "Data Source=N:\Whizzkids\WhizzkidsDB.accdb;")
  26.  
  27. 'This creates the table 'Staff' in the database
  28. SQLCommand = "CREATE TABLE Staff(StaffID AUTOINCREMENT PRIMARY KEY, StaffName VarChar(255), Username VarChar(255), PWord VarChar(255), Club VarChar(255), IsAdmin Bit);"
  29. con.Open()
  30. cmd.Connection = con
  31. cmd.CommandText = (SQLCommand)
  32. Try
  33. cmd.ExecuteNonQuery()
  34. Catch ex As Exception
  35. End Try
  36.  
  37. 'This inserts the default information so you can log on the first time
  38. cmd.CommandText = "INSERT INTO Staff(StaffName, Username, PWord, Club, IsAdmin) VALUES ('AdminName', 'Admin', '" & HashedAdminPWord & "', '', True);"
  39. cmd.ExecuteNonQuery()
  40. con.Close()
  41.  
  42. SQLCommand = "CREATE TABLE ParentLink(FamilyID VarChar(255) PRIMARY KEY, ChildNameOne VarChar(255), ChildNameTwo VarChar(255), ChildNameThree VarChar(255)," &
  43. "ChildNameFour VarChar(255));"
  44. con.Open()
  45. cmd.Connection = con
  46. cmd.CommandText = (SQLCommand)
  47. Try
  48. cmd.ExecuteNonQuery()
  49. Catch ex As Exception
  50. End Try
  51. con.Close()
  52.  
  53. SQLCommand = "CREATE TABLE Register(ChildID VarChar(255) PRIMARY KEY, Present Bit, TimeIn Time, TimeOut Time, Club VarChar(255));"
  54. con.Open()
  55. cmd.Connection = con
  56. cmd.CommandText = (SQLCommand)
  57. Try
  58. cmd.ExecuteNonQuery()
  59. Catch ex As Exception
  60. End Try
  61. con.Close()
  62.  
  63. SQLCommand = "CREATE TABLE PaymentLog(ParentID VarChar(255) PRIMARY KEY, ParentName VarChar(255), PaymentsOwed Decimal(5,2), AmountPaid Decimal(5,2), PaymentMethod VarChar(255));"
  64. con.Open()
  65. cmd.Connection = con
  66. cmd.CommandText = (SQLCommand)
  67. Try
  68. cmd.ExecuteNonQuery()
  69. Catch ex As Exception
  70. End Try
  71. con.Close()
  72.  
  73. SQLCommand = "CREATE TABLE PaymentHistory(ParentID VarChar(255), AmountPaid Decimal(5,2), TransactionDate VarChar(255), PRIMARY KEY (ParentID, TransactionDate));"
  74. con.Open()
  75. cmd.Connection = con
  76. cmd.CommandText = (SQLCommand)
  77. Try
  78. cmd.ExecuteNonQuery()
  79. Catch ex As Exception
  80. End Try
  81. con.Close()
  82.  
  83. SQLCommand = "CREATE TABLE ChildInformation(ChildID VarChar(255), ChildName VarChar(255), Age SmallInt, FamilyID VarChar(255), StaffID Integer, Club VarChar(255)," &
  84. "PRIMARY KEY (ChildName, FamilyID), FOREIGN KEY (StaffID) REFERENCES Staff(StaffID), FOREIGN KEY (FamilyID) REFERENCES ParentLink(FamilyID)," &
  85. "FOREIGN KEY (ChildID) REFERENCES Register(ChildID));"
  86. con.Open()
  87. cmd.Connection = con
  88. cmd.CommandText = (SQLCommand)
  89. Try
  90. cmd.ExecuteNonQuery()
  91. Catch ex As Exception
  92. End Try
  93. con.Close()
  94.  
  95. SQLCommand = "CREATE TABLE ParentInformation(ParentName VarChar(255), ParentID VarChar(255), FamilyID VarChar(255), ContactNumber Float, Address VarChar(255)," &
  96. "PaymentsOwed Decimal(5,2), FamilyGP VarChar(255), PRIMARY KEY (ParentName, Address), FOREIGN KEY (FamilyID) REFERENCES ParentLink(FamilyID)," &
  97. "FOREIGN KEY (ParentID) REFERENCES PaymentLog(ParentID));"
  98. con.Open()
  99. cmd.Connection = con
  100. cmd.CommandText = (SQLCommand)
  101. Try
  102. cmd.ExecuteNonQuery()
  103. Catch ex As Exception
  104. End Try
  105. con.Close()
  106.  
  107.  
  108. End If
  109.  
  110.  
  111.  
  112.  
  113. 'Creation of title
  114. Dim lblTitle As New Label
  115. With lblTitle
  116. .Size = New Size(300, 50)
  117. .Location = New Size(80, 20)
  118. .Text = "Whizzkids Staff Login"
  119. .Font = New Font("Cambria", 22)
  120. End With
  121. 'Creation of Username Box
  122. Dim txtUsername As New TextBox
  123. With txtUsername
  124. .Size = New Size(270, 20)
  125. .Location = New Size(81, 100)
  126. .Text = "Username"
  127. .ForeColor = Color.DimGray
  128. .Font = New Font("Calbri", 18)
  129. .Name = "UsernameBox"
  130. End With
  131. 'Creation of Password Box
  132. Dim txtPassword As New TextBox
  133. With txtPassword
  134. .Size = New Size(270, 20)
  135. .Location = New Size(81, 157)
  136. .Text = "Password"
  137. .ForeColor = Color.DimGray
  138. .Font = New Font("Calbri", 18)
  139. .Name = "PasswordBox"
  140. End With
  141. 'Creation of Login Button
  142. Dim btnLogin As New Button
  143. With btnLogin
  144. .Size = New Size(120, 50)
  145. .Location = New Point(160, 230)
  146. .Text = "Login"
  147. .Name = "LoginButton"
  148. End With
  149.  
  150. 'Adding all the runtime objects
  151. Me.Controls.Add(lblTitle)
  152. Me.Controls.Add(txtUsername)
  153. Me.Controls.Add(txtPassword)
  154. Me.Controls.Add(btnLogin)
  155.  
  156. 'Runs the subs when the boxes and buttons are clicked
  157. AddHandler btnLogin.Click, AddressOf LoginClick
  158. AddHandler txtUsername.Click, AddressOf ClickMouseUserName
  159. AddHandler txtPassword.Click, AddressOf ClickMousePassword
  160.  
  161. End Sub
  162. Sub ClickMouseUserName()
  163. 'This sub runs when the Username textbox is clicked.
  164.  
  165. 'This calls the class to change the password text to make it hidden
  166. Dim txtpassword As New StarText
  167. 'If the Username textbox contains 'Username' when it is clicked, that is deleted and the text colour is set to black instead of grey
  168. If Me.Controls("UsernameBox").Text = "Username" Then
  169. Me.Controls("UsernameBox").Text = ""
  170. Me.Controls("UsernameBox").ForeColor = Color.Black
  171. End If
  172. 'This places the 'Password' back in the Password textbox if it is empty when the Username box is clicked
  173. If Me.Controls("PasswordBox").Text = "" Then
  174. Me.Controls("PasswordBox").Text = "Password"
  175. Me.Controls("PasswordBox").ForeColor = Color.DimGray
  176. txtpassword.UnstarPassword(Me.Controls("PasswordBox"))
  177. End If
  178. End Sub
  179.  
  180. Sub ClickMousePassword()
  181. 'This sub does the same as the sub above, but for the other respective textboxes
  182.  
  183. Dim txtpassword As New StarText
  184. If Me.Controls("PasswordBox").Text = "Password" Then
  185. Me.Controls("PasswordBox").Text = ""
  186. Me.Controls("PasswordBox").ForeColor = Color.Black
  187. End If
  188. If Me.Controls("UsernameBox").Text = "" Then
  189. Me.Controls("UsernameBox").Text = "Username"
  190. Me.Controls("UsernameBox").ForeColor = Color.DimGray
  191. End If
  192. txtpassword.StarPassword(Me.Controls("PasswordBox"))
  193. End Sub
  194. Sub LoginClick()
  195. 'This sub validates the login with the Staff table
  196.  
  197. Dim LoginUsername As String = ""
  198. Dim LoginPassword As String = ""
  199. Dim LoginCorrect As Boolean
  200. Dim ReaderAdmin As Boolean = False
  201. Dim ReaderClub As Boolean = False
  202. Dim cat As Catalog = New Catalog()
  203. Dim con As New OleDbConnection(ConnectionString)
  204. Dim cmd As New OleDbCommand
  205.  
  206. 'This searches the staff table for the correct Username and Password and gives the user, if an Admin, the option to select which club that they are at
  207. 'Reading and writing from files - Grade B
  208. cmd.Connection = con
  209. cmd.CommandText = "SELECT Username, Pword, IsAdmin, Club FROM Staff WHERE Username = '" & Me.Controls("UsernameBox").Text & "'" &
  210. "AND PWord = '" & HashPassword(Me.Controls("PasswordBox").Text) & "'"
  211. con.Open()
  212. Dim reader As OleDbDataReader = cmd.ExecuteReader()
  213. Do While reader.Read()
  214. LoginUsername = reader("Username")
  215. LoginPassword = reader("PWord")
  216. ReaderAdmin = reader("IsAdmin")
  217. Loop
  218. If LoginUsername <> "" And LoginPassword <> "" Then
  219. LoginCorrect = True
  220. If ReaderAdmin = True Then
  221. AdminAccess = True
  222. Club_Selection.Show()
  223. Else
  224. Main_Menu.Show()
  225. End If
  226. Me.Hide()
  227. Else
  228. MsgBox("Login details incorrect. Please try again.")
  229. LoginCorrect = False
  230. End If
  231.  
  232.  
  233. reader.Close()
  234. con.Close()
  235.  
  236.  
  237.  
  238. End Sub
  239. Public Shared Function HashPassword(ByVal HPassword As String) As String
  240. 'This sub is the hashing algorithm. It hashes the password when stored and hashes it when a login is attempted, so the true password is never visible anywhere
  241. 'Hashing - Grade A
  242.  
  243. Dim HashAlgorithm As New System.Security.Cryptography.MD5CryptoServiceProvider()
  244. Dim PasswordBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(HPassword)
  245.  
  246. PasswordBytes = HashAlgorithm.ComputeHash(PasswordBytes)
  247.  
  248. Dim HashedPassword As String = ""
  249. Dim b As Byte
  250.  
  251. For Each b In PasswordBytes
  252. HashedPassword += b.ToString("x2")
  253. Next
  254.  
  255. Return HashedPassword
  256. End Function
  257.  
  258. End Class
  259. Class StarText
  260. 'These subs make the password textbox on the login visible and hidden when called
  261. Public Sub StarPassword(ByVal txtpassword As TextBox)
  262. txtpassword.UseSystemPasswordChar = True
  263. End Sub
  264. Public Sub UnstarPassword(ByVal txtpassword As TextBox)
  265. txtpassword.UseSystemPasswordChar = False
  266. End Sub
  267. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement