Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Basic Login System [TuT]

Hello HackForumers!

This is my next tutorial on how to make a basic login system with a nice little feature for registering users! I saw a few members requesting this in other topics so I thought I would just share this. It was done as a school project (Just to get us used to VB.NET) earlier on in the year. A lot of this was done by myself as I loved to get ahead and do my own features so enjoy the tutorial!

Okay so first we should always design our GUIs! Make it look the best to your own standards, don't bother caring with what other people think (Unless it was really bland..) So here is my version of my GUI.
[Image: 2piu8.png]

Okay so what we need in our form is these:
3 x Forms
3 x Buttons
2 x Labels

Pretty basic isn't it?
This is the simple version of what we have. You can name these anything you like however I've decided to name mine:
Forms: Login.vb & Success.vb & Register.vb
Buttons: btnLogin & btnReg & btnClose
Labels: txtName & txtPass

Alright so onto our coding. This isn't too hard if you know about objRead.
Code
Dim filename As String = "E:\Login_Program\Login\login.txt"  
Dim intError = 1
Dim objRead As New System.IO.StreamReader(filename)

We need to declare these variables in our btnLogin (Or Button1).
Declaring filename is just using it for objRead which is just reading the text file for the login as well as it locates the file. (It's easier this way since we just use filename from now on in the button). Next we need to declare intError which if an error of 1 happens to appear, then an error will show. Lastly we declare objRead as a StreamReader with of coarse, our declared filename! This is so we don't have to type in the filepath every time.

Next we need to be able to read the text file as well as create a loop so it reads through the text file not just one or two lines!
Code
Do While objRead.Peek() <> -1
This just reads through or simply "peeks" onto the next line.

Now we need to add an if statement for our validation process. This is so if we have the incorrect details then we won't login! We simply start off like this:
Code
If txtName.Text = objRead.ReadLine() And txtPword.Text = objRead.ReadLine() Then
This is saying if the txtName and txtPword is equal to the objRead then it'll read the next line of code which is this:
Code
Me.Hide()
    Success.Show()
    intError = 0
    End If
Me.Hide() closes the current form and shows the next form which for us is Success.Show() (You can have many forms you need but it's up to you really). We also have intError = 0 which lets the program know that there is no errors!

Now we need to finish off our loop so we know that it'll read throughout the whole text file. We simply add this code after it:
Code
Loop
Now we need to know that if the details are correct, we need to have an error which will be displayed as:
Code
If intError = 1 Then 'If an error has occured'
MessageBox.Show("Incorrect Username or password", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error)
This lets the program know that the error is equal to 1, so we need to show a message box which lets the user know that the login details are incorrect.

Lastly we need to close the objRead so the program doesn't infinite loop and eventually crash! We do this by adding:
Code
End If
objRead.Close()

Also add for your register button:
Code
Me.Hide()
Register.Show()

This is so we can do our next part of registering new users!

Now that was the first form out of two for coding (The third is only just a welcome screen which I couldn't be bothered using). If the login details are correct then my Success form will show like so:
[Image: ArzUy&x2V2al&2piu8]

Now it's time for coding the register details for the program!
First we should design the register form, so mine looks like this:
[Image: ArzUy.png]

I simply have:
2 x Buttons
3 x Labels
3 x Textboxes

(You can name these yourselves as you should have the simple knowledge on using these!)

This is because I added a little validation, if the two passwords aren't the same, then the program returns an error. You don't have to have this added but it'll be added into this source. So go click on your register form and lets get started.

First we need to declare two more variables:
Code
Dim filename As String = "login.txt"
Dim objWrite As New System.IO.StreamWriter(filename, True)
First I need to explain objWrite and StreamWriter. This writes to the text file which we declared with filename (Tips will be down the bottom). Using true, this just writes to that file. Anyways this is all we need for this part of the coding, not much huh?

This is for our register button on the register form, so we need to add the validation sequence and if it's correct then objWrite will write the username and password to the text file, like so in the code below:
Code
If txtPass.Text = txtRPass.Text Then
objWrite.WriteLine(txtUser.Text) 'Write username'
objWrite.WriteLine(txtPass.Text) 'Write password'
Now we need to add validation if it's incorrect.
Code
If txtUser.Text = "" And txtPass.Text = "" Then
MessageBox.Show("No username/password entered!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Add a little validation if they're left blank! Now we need to close the objWrite otherwise the program can crash. Do this by adding:
Code
objWrite.Close()
Exit Sub
Exiting the sub with Exit Sub just lets the program stop itself. Now we need to finish off the IF statement so lets do this by adding:
Code
Else
End If
That wasn't so hard, was it?

Add a message box to let the user that the account is created just for personal use!
Code
MessageBox.Show("The new account has been created!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
Otherwise if there is an error with the passwords, then show an error by adding:
Code
Else
MessageBox.Show("Passwords are the same", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Now we can finish the program by adding:
Code
End If
objWrite.Close()
End Sub

The program seems pretty simple, doesn't it?
Hints and Tips:
Try declaring the declarations above the the buttons just under Form1. This'll let it know that they're Global (So they can be used all over the form, not just where they're specified). Try it out yourself and let me know how this program is.

Code
Dim filename As String = "file.txt"
To get this, you should drop the .txt file in the bin folder.

Thanks again!
My program will be added here later.. But there isn't much to it.

Happy Coding!
-Lenneth
There are better methods but good tut if it was not a copy paste :)
(Sep 5, 2011 05:45 PM)NinjaxDGx Wrote:  There are better methods but good tut if it was not a copy paste :)

Like I said, basic.
And don't assume it's a copy paste as that is my program, not letting people download the source. I hate copy/pasting, took me nearly an hour to write this up.. While Im still at school hehe.
(Sep 5, 2011 06:16 PM)Lenneth Wrote:  Like I said, basic.
And don't assume it's a copy paste as that is my program, not letting people download the source. I hate copy/pasting, took me nearly an hour to write this up.. While Im still at school hehe.

OMG,
Quote:While Im still at school hehe.

How did you get so skillfull? Can you teach me your coolness? I didn't make VB.net Programs sitting at school while the teacher wasn't looking.
You tank.
(Sep 5, 2011 06:54 PM)deiski Wrote:  OMG,
How did you get so skillfull? Can you teach me your coolness? I didn't make VB.net Programs sitting at school while the teacher wasn't looking.
You tank.

How.. Hurtful?
Flame all you want, I gave what users wanted. If you don't have any criticism involved then why not bother elsewhere :)
What the hell? Why would you save it on E drive???
90% HF users are still at school dear and some of them make more money than your parents does.
(Sep 5, 2011 11:27 PM)Coleak Wrote:  What the hell? Why would you save it on E drive???

Where my USB is located, however I forgot to drop it in the 'bin' folder.
Lenneth you did a good job but this can be SO easily cracked lol
(Sep 6, 2011 12:53 AM)The Unknown Wrote:  Lenneth you did a good job but this can be SO easily cracked lol

Encryption is what I'll sometime learn soon however users who make this can experiment for themselves.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by Cherry.Oct 8, 2013 09:41 AM 1 340 Oct 8, 2013 09:41 AM
Last Post: Onlykl
[Python] by ScrewbaccaAug 11, 2013 08:54 AM 10 464 Aug 11, 2013 08:54 AM
Last Post: arkoydas
[Python] by ScrewbaccaAug 10, 2013 02:24 PM 16 772 Aug 10, 2013 02:24 PM
Last Post: arkoydas