Introduction
This article should be useful for those who need a simple username/password solution for their VB Projects.
Background
I'm only a noob to VB programming and this is my first CodeProject article. I searched the internet for ages, just looking for some good example code, but could not find any. So I had to start from scratch and in the end result I ended up using VB, LINQ and SQL. I use Visual Studio 2008 and Microsoft SQL Server 2008 Express Edition with full text searching installed on top of Windows XP Professional SP3. Don't know if this helps but just thought I would tell you what platform I am using.
Using the Code
All the code below is the background code for the login form. The form calls upon a SQL database via LINQ.
The main table I call upon is called "users
" and the main columns that I query upon are the "UserName
", "PssWrd
" and "UserActive
". You can call all of these columns anything you want.
The username
and password
columns are nvchar(25)
s and the useractive
column is nvchar(5)
. The reason for nvchar(5)
on the useractive
column is because I like to use boolean logic and I couldn't figure out how to make a yes or no column in SQL, so I just throw in a true
or false
into the data tables for my users.
Public Class Login
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles OK.Click
Me.Check_Details()
End Sub
This Button calls Check_Details Sub
to verify user credentials.
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Cancel.Click
Me.Close()
End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Me.UsersTableAdapter.FillBy(Me.SecurityDataSet1.Users)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
On Form load, this code tells the form to load database details and fill the appropriate boxes (On this form, it fills the UserNameComboList
Box).
Private Sub Check_Details()
Dim UName As String = Me.UserNameBox.Text.ToString.Trim
Dim PWord As String = Me.PassWordBox.Text.ToString.Trim
Dim sd As New SecurityDataSetTableAdapters.UsersTableAdapter
Dim query = From check In sd.GetData _
Select check.LogonName, check.PssWrd, check.UserActive _
Where LogonName = UName AndAlso PssWrd = _
PWord AndAlso UserActive = "True"
If query.Count() = 1 Then
MessageBox.Show("confirmed")
Else
MessageBox.Show("Password Is Incorrect or no longer Valid", _
"Password Problem")
End If
End Sub
This command checks to see if the inputted user credentials are correct. The declarations convert the text in the form's boxes into string
values for use with LINQ SQL Queries. The second set of declarations convert the tableadapter sequence into a short string
name for use with our SQL Query. The Query checks whether or not the inputted username and password match. It also validates if the current user is Active or Inactive.
The IF
command executes our SQL Query and tells it to count how many replies there are. If the count does not produce a response of 1 (Which is what it should come up with), it will fall over to the second part of the IF
command.
If the result is positive, the IF
command will run any command under it.
Private Sub unmaskpw_CheckedChanged_
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles unmaskpw.CheckedChanged
If unmaskpw.Checked = True Then
Me.PassWordBox.PasswordChar = ""
Else
Me.PassWordBox.PasswordChar = "$"
End If
End Sub
End Class
Here I have a Check box on my form that turns on/off password masking.
I hope you found this information and code helpful. Later I will probably work on this a bit more by adding a security management section and hashing/encryption for the password.
Points of Interest
I found that I can manipulate VB code easier than PHP. I might fool around with it a bit more.
History
- 21 June 2009 -- First initial article... probably lots of mistakes. I will do better next time.