Advertisement
ElliottE4

Untitled

Feb 18th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.54 KB | None | 0 0
  1. Imports System.IO 'imports library to allow the use of stream reader/writer
  2.  
  3. Public Class ForgottenPassword
  4.  
  5.     Dim securityAnswer As String 'creates new string variable to store the security answer of the user
  6.     Dim password As String 'creates new string variable to store the password of the user
  7.     Dim securityQuestion As String 'creates new string variable to store the security question of the user
  8.     Private Sub pbx_ExitButton_Click(sender As Object, e As EventArgs) Handles pbx_ExitButton.Click
  9.  
  10.         If MsgBox("Are you sure you want to exit to the login form?", vbYesNo) = vbYes Then
  11.  
  12.             'when the exit button is clicked, exit the program if yes is selected
  13.             Dim Login As New Login 'creates new login form
  14.             Login.Show() 'shows the login form
  15.             Me.Close() 'exits the current form
  16.  
  17.  
  18.         End If
  19.  
  20.     End Sub
  21.  
  22.     Private Sub btn_Search_Click(sender As Object, e As EventArgs) Handles btn_Search.Click
  23.  
  24.         Dim username As String 'creates a string variabe to store the username
  25.         Dim currentUser As String 'creates a string variable to store the current user information
  26.         Dim loginFields() As String 'creates an array to store the information input into the login fields
  27.         Dim match As Boolean = False 'creates a boolean variable to check if username and password match later on, set to false to begin with
  28.  
  29.  
  30.  
  31.  
  32.         username = UCase(txt_Username.Text) 'sets the username variable to an uppercase version of the username in textbox
  33.  
  34.         If username = "" Then 'if the username textbox is clear
  35.  
  36.             MsgBox("Enter your username into the username texbox to search for password") 'shows message to ask user to input data into username texbox
  37.             txt_Username.Clear() 'clears username textbox
  38.  
  39.         End If
  40.  
  41.         Dim sr As New StreamReader(staffFile) 'opens streamread, to allow the staff file to be read
  42.         While sr.Peek >= 0 'while the file hasn't reached the end:
  43.  
  44.             currentUser = sr.ReadLine 'reads the the first line of the file, then each loop reads the next line
  45.             loginFields = currentUser.Split(",") 'the line is split into different sections by splitting at every comma
  46.  
  47.             If username = UCase(Decryption(loginFields(1))) Then 'if the data in the textbox matches the first field of the currently selected record in the login text file, then:
  48.  
  49.                 'match has been found:
  50.                 securityQuestion = Decryption(loginFields(6)) 'the security quesiton variablehas the data from the corresponfing field stored in it
  51.                 securityAnswer = Decryption(loginFields(7)) 'the security answer variable has the data from the corresponfing field stored in it
  52.                 password = Decryption(loginFields(2)) 'the password variable has the data from the corresponfing field stored in it
  53.  
  54.                 Security = Decryption(loginFields(5)) 'sets the access level to the lowest to allow the next form to be accessed
  55.                 match = True 'checks if the input data matches any data in the textfile
  56.                 Exit While 'ends the loop as a match has been found
  57.  
  58.             End If
  59.  
  60.         End While
  61.  
  62.         sr.Close() 'closes streamwriter to avoid data loss
  63.  
  64.         If match = True Then 'if a match has been found:
  65.  
  66.             MsgBox("Account found. Please answer your security question below") 'shows a message box to tell the user that the account is found
  67.             txt_SecurityQuestion.Text = securityQuestion 'puts the data stored in the securityQuesiton variable in the textbox
  68.  
  69.         Else
  70.  
  71.             MsgBox("No match found. Please try again with a different username") 'shows a message box to tell the user that a match hasn't been found
  72.  
  73.             'clears all textboxes
  74.             txt_Username.Clear()
  75.             txt_Answer.Clear()
  76.             txt_SecurityQuestion.Clear()
  77.  
  78.         End If
  79.  
  80.     End Sub
  81.  
  82.     Private Sub btn_Enter_Click(sender As Object, e As EventArgs) Handles btn_Enter.Click
  83.  
  84.         If txt_Answer.Text = securityAnswer Then 'if the answer entered by the user matches the correct answer, then:
  85.  
  86.             MsgBox("Security answer is correct. Your password is:" & password) 'shows a message box to tell the user thier password
  87.             MsgBox("Please log in again.") 'shows a message box to prompt the user to log in again
  88.             Me.Visible = False 'hides the current form
  89.             Login.Visible = True 'shows the login form
  90.  
  91.             'clears data in textboxes in case the form is reopened
  92.             txt_Username.Clear()
  93.             txt_Answer.Clear()
  94.             txt_SecurityQuestion.Clear()
  95.  
  96.         Else
  97.  
  98.             MsgBox("Security answer is incorrect. Please try again.") 'shows a message box to tell the user that their answer was incorrect
  99.             txt_Answer.Clear() 'clears the answer textbox
  100.  
  101.         End If
  102.  
  103.     End Sub
  104.  
  105.     Private Sub btn_Help_Click(sender As Object, e As EventArgs) Handles btn_Help.Click
  106.  
  107.         Help.txt_help.Text = "This is the forgotten password form. You will need to input your username into the username textbox, then click the search button. " _
  108.             & "If the username matches an existing one in the database, the security question linked to that account will be shown. Answer this question in the security answer textbox below. " _
  109.             & "If the answer is correct, your password will be shown on screen. If it is incorrect, you can either try again, or exit back to the login form using the cross button in the top right"
  110.  
  111.         Help.Show()
  112.  
  113.     End Sub
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement