|
-
Jul 13th, 2011, 08:56 PM
#1
Thread Starter
Junior Member
[RESOLVED] Login from text file?
Hi all,
I'm making a virtual OS, but I need some help with the login code.
This is what I have so far:
Code:
Public Class Login
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Label1.Visible = False
Dim Test1 As String = UsernameTextBox.Text
Dim Test2 As String = TextBox1.Text
If AllUsers.Contains(Test1) Then
If AllPasswords.Contains(Test2) Then
'ADD LOGIN
MsgBox("works")
End If
Else
Label1.Visible = True
End If
End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim file As New System.IO.StreamReader("Users.txt")
Dim Length As Integer = file.ReadLine()
Dim Users(Length) As String
For i = 0 To Length - 1
Users(i) = file.ReadLine()
Next
AllUsers = Users.ToString()
file.Close()
Dim file1 As New System.IO.StreamReader("x86VarServerX10.txt")
Dim L2 As Integer = file1.ReadLine()
Dim Passwords(L2) As String
For i = 0 To L2 - 1
Passwords(i) = file1.ReadLine()
Next
MsgBox(Passwords)
MsgBox(Users)
End Sub
End Class
My problem(s) are that the arrays are getting an extra value ;eg length = 3 instead of 2 (which is specified in the text files), so I end up with a 3rd value "Nothing". This is bad because someone could login without a user/password.
My other problem is that the arrays error in the msgbox. Using ToString() results in "System.String[]". How can I fix this?
-
Jul 14th, 2011, 01:30 AM
#2
Re: Login from text file?
This is probably a good way to learn, although 'virtual OS' is a bit grand title for what you are trying to do 
Your first problem seems to be in the fact that you didn't turn Option Strict on. Do that right now and don't ever turn it off.
Code:
Dim Length As Integer = file.ReadLine()
the ReadLine method returns a System.String while you try to assign a string value to an Integer variable without even checking what's been in that string.
Secondly, the last call to the ReadLine will most probably return an empty string (a common thing with text files is that the last line is empty) so you should check for that.
Also, if you need to get contents of the file in the string array you can use IO.File.ReadAllLines() method which will return you a string array.
Third. You don't seem to be checking for the existance of the text files you try to open. This may cause an run-time error if something happens with these files or they will be locked by some other problem.
All I/O operations should be enclosed in Try....Catch...End Try block and handle possible exceptions like 'File not found', 'Access denied', etc.
Four. Storing user names and passwords separately is a good way to mess the things up. Storing the passwords in an unencrypted form is the same as not to have password protection at all. Usernames/passwords must be kept in one place and always be encrypted.
Five. Consider using a database instead of text files.
-
Jul 14th, 2011, 12:27 PM
#3
Thread Starter
Junior Member
Re: Login from text file?
Thanks for all the info. Right now, though, I'm not really going deep with this. It's just a test, when I get everything down I will start using encryption methods. As for databases, I am extremely new at it, and I would really be glad if you pointed me to a beginner project to learn.
Now, for the string to integer problem - should I use CInt() on it before assigning it?
Lastly - for the Try statement - they're not there because I know they will be. Again, when I finish the whole thing, that's when I'll go into more detail on the whole security and debugging aspects.
-
Jul 14th, 2011, 01:11 PM
#4
Thread Starter
Junior Member
Re: Login from text file?
This is my code so far:
vb Code:
Option Strict On Public Class Login Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click Dim user As String = UsernameTextBox.Text.ToLower() Dim pass As String = TextBox1.Text.ToLower() If CBool(Array.BinarySearch(AllUsers, user)) = False Then MsgBox("works") End If End Sub Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim users As Array = IO.File.ReadAllLines("Users.txt") AllUsers = users Dim passes As Array = IO.File.ReadAllLines("x86VarServerX10.txt") AllPasswords = passes End Sub End Class
My problem now is that the username only works for the first user; In my text file I state two users:
Guest
Admin
When I try to login as Guest, I get the messagebox, with Admin, I don't get anything. What am I doing wrong?
-
Jul 14th, 2011, 02:27 PM
#5
Re: Login from text file?
The problem is with the way you perform the search.
Read the documentation carefully:
 Originally Posted by MSDN
Return Value
Type: System.Int32
The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
Then you convert the return value to the Boolean one with CBool. Fine thing with CBool function and boolean values in general is that it considers zero to be a false value while all others as true.
The binary search returns zero as the index of the found element, the element is found - fine, but when you convert CBool(0) = False.
Instead just check if a return value is greater than or equal to zero.
P.S.
An Array.IndexOf() method perhaps fits here better than BinarySearch and the readability of the code will he better.
P.P.S.
If you still want to store user names and passes separately you could at least check if
Code:
AllUsers.Length = AllPasswords.Length
P.P.P.S
Also, ALWAYS catch possible exceptions when you do file i/o. Disk errors, missing files, access problems and other thing may crush the program at unexpected time.
-
Jul 15th, 2011, 01:17 PM
#6
Thread Starter
Junior Member
Re: Login from text file?
vb Code:
Option Strict On Public Class Login Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click Dim user As String = UsernameTextBox.Text.ToLower() Dim pass As String = TextBox1.Text.ToLower() If CBool(Array.IndexOf(AllUsers, user)) Then MsgBox("works") End If End Sub Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim users As Array = IO.File.ReadAllLines("Users.txt") AllUsers = users Dim passes As Array = IO.File.ReadAllLines("x86VarServerX10.txt") AllPasswords = passes End Sub End Class
Thank you for all the help so far. But I am still not understanding what I should do. If you could please give me an example of something that works, I would be very greatful.
-
Jul 16th, 2011, 08:06 PM
#7
Thread Starter
Junior Member
Re: Login from text file?
-
Jul 17th, 2011, 02:37 AM
#8
Re: Login from text file?
Presumably you've defined AllUsers and AllPasses as global variables as they aren't declared in your class (I hope you have Option Explicit on otherwise as per cicatrix's advice with Option Strict turn it on now and leave it on).
What are you expecting this code to do?
Code:
If CBool(Array.IndexOf(AllUsers, user)) Then
MsgBox("works")
End If
You need to read what cicatrix posted above - IndexOf is a slightly more complicated function than you think. It doesn't readily convert into true/false. Try this code for example :
Code:
Dim AllUsers() As String = {"Guest", "Admin"}
If CBool(Array.IndexOf(AllUsers, "Guest")) Then MsgBox("Guest works")
If CBool(Array.IndexOf(AllUsers, "Admin")) Then MsgBox("Admin works")
If CBool(Array.IndexOf(AllUsers, "Blah")) Then MsgBox("Blah works")
Guest doesn't "work" because the index of "Guest" in the array is zero which CBool converts to false.
Admin "works" because the index of "Admin" in the array is one which is nonzero and so CBool converts to true.
Blah "works" because "Blah" doesn't exist in the array and IndexOf returns -1 which CBool converts to true.
Cicatrix gave you the solution to this problem above.
"Instead just check if a return value is greater than or equal to zero."
Code:
Option Strict On
Public Class Login
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim user As String = UsernameTextBox.Text.ToLower()
Dim pass As String = TextBox1.Text.ToLower()
If Array.IndexOf(AllUsers, user) >= 0 Then
MsgBox("works")
End If
End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim users As Array = IO.File.ReadAllLines("Users.txt")
AllUsers = users
Dim passes As Array = IO.File.ReadAllLines("x86VarServerX10.txt")
AllPasswords = passes
End Sub
End Class
-
Jul 17th, 2011, 06:42 AM
#9
Thread Starter
Junior Member
Re: Login from text file?
Oh, ok. Thank you all very much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|