How can I retrieve single user data from ZKTeco device? I was able to retrieve all the data into a Listbox, but now I want to load the data from a single user to Textboxes using a Textbox with the user ID.
I tried:
Private Sub txtUserID_TextChanged(sender As Object, e As EventArgs) Handles txtUserID.TextChanged
If bIsConnected = False Then
MsgBox("Please connect the device first", MsgBoxStyle.Exclamation, "Error")
Return
End If
Dim sdwEnrollNumber As String = ""
Dim sName As String = ""
Dim sPassword As String = ""
Dim iPrivilege As Integer
Dim bEnabled As Boolean = False
Dim sCardnumber As String = ""
lbRTShow.Items.Clear()
lbRTShow.BeginUpdate()
Cursor = Cursors.WaitCursor
axCZKEM1.EnableDevice(iMachineNumber, False)
axCZKEM1.ReadAllUserID(iMachineNumber)
While axCZKEM1.SSR_GetUserInfo(iMachineNumber, sdwEnrollNumber, sName, sPassword, iPrivilege, bEnabled) = True 'get user information from memory
If axCZKEM1.GetStrCardNumber(sCardnumber) = True Then
txtCardnumber.Text = sdwEnrollNumber
txtName.Text = sName
txtPassword.Text = sPassword
txtUserID.Text = sCardnumber
End If
End While
axCZKEM1.EnableDevice(iMachineNumber, True)
lbRTShow.EndUpdate()
Cursor = Cursors.Default
End Sub
But doesn't return anything.
Make sure to pass the parameter with "out" keyword for sName, sPAssword, iPriviege and bEnabled.
axCZKEM1.SSR_GetUserInfo(iMachineNumber, sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled)
You can try and let me know, if it works.
Related
I'm working on a program in VB.net that uses the form load event to prompt for password. I have this working, but I am supposed to be showing the attempt number you are at if you fail. However, my code is always returning "Attempt #1" and never increasing to #2, #3, etc, and I am unsure why it is constantly being reset.
Private Function checkPassword(passwordGuess As String)
Dim isValid As Boolean = False
Dim password As String = "941206"
Dim attemptCounter As Integer
If isValid = False Then
If password <> txtPassword.Text Then
attemptCounter += 1
MessageBox.Show("Login Unsuccesful.",
"Attempt #" & attemptCounter)
Else
isValid = True
MessageBox.Show("Login Successful.",
"Attempt #" & attemptCounter)
Me.Text = "Attempt #" & attemptCounter
End If
End If
End Function
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
Dim password As String
password = txtPassword.Text
checkPassword(password)
End Sub
You could create a class to store information about attempts that persists across function calls:
Public Class attempt
Public Shared counter As Integer = 0
End Class
Private Function checkPassword(passwordGuess As String)
Dim isValid As Boolean = False
Dim password As String = "941206"
If isValid = False Then
If password <> txtPassword.Text Then
attempt.counter += 1
MessageBox.Show("Login Unsuccesful.",
"Attempt #" & attempt.counter)
Else
isValid = True
MessageBox.Show("Login Successful.",
"Attempt #" & attempt.counter)
Me.Text = "Attempt #" & attempt.counter
End If
End If
End Function
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
Dim password As String
password = txtPassword.Text
checkPassword(password)
End Sub
I am having problems figuring out in which order these if and else statements need to be arranged in Visual Basic 2010 Express. As you can probably tell from the code, when it is working properly the program should display data found in a text file externally. If this data is not found then an error message in a label should appear.
Thanks for the help.
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filename As String = "..\..\usernamepassword.txt"
Dim objreader As New System.IO.StreamReader(filename)
Dim contents As String
Dim check As String
Dim checkfile As New System.IO.StreamReader("..\..\checkfile.txt")
check = checkfile.ReadToEnd
For i As Integer = 1 To check
contents = objreader.ReadLine
Dim data As New List(Of String)(contents.Split(","))
If forename.Text = data(2) Then
'first if statement is here \/
If surname.Text = data(3) Then
fullname.Text = (data(2) & " " & data(3))
dob.Text = data(4)
phone.Text = data(5)
address.Text = data(6)
password.Text = data(1)
'else statement \/
Else
Label2.Text = "Sorry, no result found for this student."
Label5.Text = "Please note this system is caps sensitive."
End If
End If
Next i
Dim s As String = ""
forename.Text = s
surname.Text = s
End Sub
End Class
You should handle it like this
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filename As String = "..\..\usernamepassword.txt"
Dim found as Boolean = False
Dim objreader As New System.IO.StreamReader(filename)
Dim contents As String
Dim check As String
Dim checkfile As New System.IO.StreamReader("..\..\checkfile.txt")
check = checkfile.ReadToEnd
For i As Integer = 1 To check
contents = objreader.ReadLine
Dim data As New List(Of String)(contents.Split(","))
If forename.Text = data(2) Then
'first if statement is here \/
If surname.Text = data(3) Then
fullname.Text = (data(2) & " " & data(3))
dob.Text = data(4)
phone.Text = data(5)
address.Text = data(6)
password.Text = data(1)
found = True
Exit For
End If
End If
Next i
If found = False Then
Label2.Text = "Sorry, no result found for this student."
Label5.Text = "Please note this system is caps sensitive."
End If
Dim s As String = ""
forename.Text = s
surname.Text = s
End Sub
Check has been declared as string when it should be an integer.
Set option strict to on and infer to off
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = Cashierpath
System.IO.File.Exists(FILE_NAME) ' current
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
'Global Variable
'Dim DirPath7 As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Scrap Data\Cashier Info\Cashiers\")
For Each filename As String In IO.Directory.EnumerateFiles(DirPath7, "*.txt")
Dim fName As String = IO.Path.GetFileName(filename)
If user = fName & ".txt" Then
objReader = New StreamReader(fName)
check = objReader.ReadToEnd()
If password = check Then
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
Else
MessageBox.Show("Username or Password is incorrect")
End If
End If
Next
End Sub
When the user enters their "username" and "password" into the textbox's, and clicks on this button, i want this button to check if there is a textfile with the name of the username entered, and if theres a file with that username it must then read it and check if the password matches the string inside the file. if it doesnt match it, it must then display a messagebox saying that "Username or password is incorrect", but nothing happens when i click on this button. No error message appears either.
Can someone take a look at my code and tell me what im doing wrong?
What you have there is an awful way to handle user credentials!
Read this for more information: Salted Password Hashing - Doing it Right
Regardless, you're way over-coding it.
Elsewhere in your app (correct use of Combine):
' Global Variable
Friend Shared DirPath7 As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Scrap Data", "Cashier Info", "Cashiers")
Button Handler:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim User As String = TextBox1.Text.Trim
Dim Pass As String = TextBox2.Text.Trim
' Assemble the full expected file path, even if it might be malformed.
' The first case check protects against malformed path.
Dim FilePath As String = IO.Path.Combine(DirPath7, String.Format("{0}.txt", User))
Select Case True
Case User.Length = 0
MsgBox("Username is empty", vbExclamation, "Error")
Case Pass.Length = 0
MsgBox("Password is empty", vbExclamation, "Error")
Case Not IO.File.Exists(FilePath)
MsgBox("No file for User", vbExclamation, "Error")
Case Not IO.File.ReadAllText(FilePath) = Pass
MsgBox("Wrong Password", vbExclamation, "Error")
Case Else
MsgBox(String.Format("Welcome {0}!", User), vbOKOnly, "Success")
My.Forms.Home.Show()
End Select
End Sub
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
Dim fname = Path.Combine(DirPath7, String.Format("{0}.txt", user))
If File.Exists(fname) Then
Using objreader As New StreamReader(fname)
'objReader = StreamReader(fname)
check = objreader.ReadToEnd()
password = check
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
End Using
Else : MessageBox.Show("file not found, no user exists")
End If
removed the extra ".txt"
Added "Do Using" . . ."End Using"
I am running a vb.net query into LDAP. I pull back the homedirectory. How can i write an IF statement to popup a msgbox if the homedirectory is empty versus the error "Object reference not set to an instance of an object."
code is below:
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If TextBox2.Text = "" Then
MsgBox("Please enter a Network ID")
TextBox2.Focus()
Exit Sub
End If
Dim yourUserName As String = TextBox2.Text
Dim ADSearch As New DirectorySearcher()
Dim de As DirectoryEntry = GetDirectoryEntry()
ADSearch.SearchRoot = de
ADSearch.Filter = "(sAMAccountName=" & yourUserName & ")"
'ADSearch.PropertiesToLoad.Add("homedirectory")
Dim ADResult As SearchResult = ADSearch.FindOne()
If ADResult Is Nothing Then
MsgBox("User not found, please try again", MsgBoxStyle.OkOnly, "Not Found")
TextBox2.Text = ""
TextBox2.Focus()
Exit Sub
Else
Dim ADEntry As DirectoryEntry = New DirectoryEntry(ADResult.Path)
TextBox1.Text = (ADEntry.Properties("homeDirectory").Value.ToString)
End If
End Sub
You need to perform very basic null checking!
' if the "homeDirectory" hasn't been set -> then it will not show up in the SearchResult
If ADEntry.Properties("homeDirectory") Is Nothing Then
' do something
Else
If ADEntry.Properties("homeDirectory").Value Is Nothing Then
' do something
Else
' access your property here
TextBox1.Text = (ADEntry.Properties("homeDirectory").Value.ToString)
End If
End If
Man I've been killing myself trying to figure this out for the past 2 days. Just when i thought i had it figured out, nope, lol. So here's my dilemna, I have a form that when a user puts in a user ID, 2 text boxes are to be filled with their Given Name / SurName. I have my code so that it will connect to the LDAP and verify whether or not the UserID is correct. Where i'm having problems is adding the names to the text boxes.
This is the boolean that connects to AD:
Public Shared Function UserExists(ByVal username As String) As Boolean
Dim answer As Boolean = False
Dim dirEntry As DirectoryEntry = Nothing
Dim ldapPath As String = "LDAP://(Insert LDAP nonsense here)"
Dim dirSearcher As DirectorySearcher = Nothing
Dim result As SearchResult = Nothing
Try
dirEntry = New DirectoryEntry(ldapPath)
dirSearcher = New DirectorySearcher(dirEntry)
With dirSearcher
.Filter = "(CN=" & username & ")"
.PropertyNamesOnly = True
.PropertiesToLoad.Add("Name")
.PropertiesToLoad.Add("GN")
.PropertiesToLoad.Add("SN")
result = .FindOne()
End With
If Not result Is Nothing Then
answer = True
End If
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
dirEntry = Nothing
dirSearcher = Nothing
End Try
Return answer
End Function
Here's the code for the button when the user hits verify:
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
Dim userID As String = TextBox18.Text
If UserExists(userID) Then
MsgBox("WooHoo")
Else
MsgBox("Fail")
End If
Textbox16.text = SN
Textbox17.text = GN
Any help would be GREATLY appreciated. Cheers
You should change .PropertiesToLoad.Add("GN") by .PropertiesToLoad.Add("givenName") The right attribute name must be use in ADSI.
Then you shoul return resul by reference something like (I do not use VB for years now, so I hope somebody will comment the syntax):
Public Shared Function UserExists(ByVal username As String, ByRef result As SearchResult) As Boolean
Dim answer As Boolean = False
Dim dirEntry As DirectoryEntry = Nothing
Dim ldapPath As String = "LDAP://(Insert LDAP nonsense here)"
Dim dirSearcher As DirectorySearcher = Nothing
Try
dirEntry = New DirectoryEntry(ldapPath)
dirSearcher = New DirectorySearcher(dirEntry)
With dirSearcher
.Filter = "(CN=" & username & ")"
.PropertyNamesOnly = True
.PropertiesToLoad.Add("Name")
.PropertiesToLoad.Add("givenName")
.PropertiesToLoad.Add("SN")
result = .FindOne()
End With
If Not result Is Nothing Then
answer = True
End If
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
dirEntry = Nothing
dirSearcher = Nothing
End Try
Return answer
End Function
Then you should call it with :
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
Dim userID As String = TextBox18.Text
Dim result As SearchResult = Nothing
If UserExists(userID, result) Then
MsgBox("WooHoo")
Else
MsgBox("Fail")
End If
Textbox16.text = result.sn
Textbox17.text = result.givenName
Figured it out in case anyone ever have the same question:
What this essentially does is after I put in the User ID, and click away, it populates the next 2 fields with the first name last name. essentially you can add the same thing to the Button
Private Sub TextBox3_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
Dim deSystem As New DirectoryEntry("LDAP:")
Dim dsSystem As New DirectorySearcher(deSystem)
Dim srsystem As SearchResult
If TextBox3.Text = Nothing Then
Exit Sub
Else
Try
dsSystem.Filter = "sAMAccountName=" & TextBox3.Text
dsSystem.PropertiesToLoad.Add("mail") 'email address
dsSystem.PropertiesToLoad.Add("department") 'dept
dsSystem.PropertiesToLoad.Add("physicalDeliveryOfficeName") 'office
dsSystem.PropertiesToLoad.Add("title") 'title, eg programmer1
dsSystem.PropertiesToLoad.Add("telephoneNumber") 'phone
dsSystem.PropertiesToLoad.Add("streetAddress") 'street address
dsSystem.PropertiesToLoad.Add("l") 'city
dsSystem.PropertiesToLoad.Add("st") 'state
dsSystem.PropertiesToLoad.Add("postalCode") 'zip code
dsSystem.PropertiesToLoad.Add("EmployeeId") 'empid
dsSystem.PropertiesToLoad.Add("givenName") '//first name from active directory
dsSystem.PropertiesToLoad.Add("sn") '//lastname from active directory
srsystem = dsSystem.FindOne()
TextBox1.Text = srsystem.Properties("givenName").Item(0).ToString
TextBox2.Text = srsystem.Properties("sn").Item(0).ToString
Catch ex As Exception
MsgBox("Invalid UserID")
End Try
End If
End Sub