Click here to Skip to main content
Click here to Skip to main content
Go to top

VB LINQ SQL Password Verify For Beginners

, 22 Jun 2009
Rate this:
Please Sign up or sign in to vote.
How to make a password verification form in VB that uses LINQ and SQL
Example.JPG

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SteveHarrisBeast

Australia Australia
No Biography provided

Comments and Discussions

 
Questionnvarchar(5)? PinmemberSpirch25-Jun-09 11:24 
AnswerRe: nvarchar(5)? PinmemberSteveHarrisBeast28-Jun-09 20:42 
AnswerRe: nvarchar(5)? Pinmemberjkhuang6-Nov-10 2:32 
GeneralMy 2 cents PinmemberAndre Luiz V Sanches22-Jun-09 16:21 
As a suggestion, keep in mind to always encrypt the password before storing it in the database, as opposed to plain text as you're doing in this article.
 
The .NET framework provides easy access to a number of encryption algorithms out-of-the-box that are very easy to use. This is minor extra effort that will help improve security in your application, and you may even earn brownie points with your customers Smile | :)
 
---------
Andre Sanches
 
"UNIX is friendly, it's just picky about its friends"

GeneralRe: My 2 cents PinmemberSteveHarrisBeast28-Jun-09 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 22 Jun 2009
Article Copyright 2009 by SteveHarrisBeast
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid