Thursday, November 10, 2011
Add Fingerprint Scan-based Security to Your .NET Applications
iometric recognition is one of the most reliable ways to confirm the identity of an individual. And by now, many people are undoubtedly familiar with theMicrosoft Fingerprint Reader (see Figure 1).
Besides logging into a computer using a fingerprint scan from the reader, you can also use the application provided by the Fingerprint Reader to save your user IDs and passwords for web sites that require them. You can then use your fingerprint as a key to retrieve the authentication information to log in securely, thereby eliminating the hassle of remembering different sets of passwords for different sites.
In the default Form1, populate it with the following controls (see also Figure 5):
In addition, add a Timer control to the project. The Timer control will be used to clear away the user's information after his identity has been loaded from the database (after five seconds).
Unfortunately, that's all you can do with the Fingerprint Reader. Microsoft does not provide an SDK to allow developers to incorporate the Fingerprint Reader into their applications. For this, you have to reply on third-party solution providers. Fortunately, there is one such provider available in the market: (Griauleprovides the GrFinger Suite, a fingerprint recognition suite that comes with an SDK for integrating your fingerprint readers into your applications. (It works with Microsoft Fingerprint Reader, Digital Persona U.are.U 4000, SecuGen Hamster FDU02, Geomok (Testech) Bio-I, and Crossmatch USB Fingerprint Readers.).
In this article, I will show you how you can use the GrFinger SDK to integrate the Microsoft Fingerprint Reader into your .NET applications. In particular, you will build a Visitor Identification System whereby users visiting your office can register with their fingerprints at the reception desk (see Figure 2). Once a user is registered, the next time he visits the office he can simply scan his fingerprint and the system will register his visit. This application can also be adapted by schools for attendance taking purposes, such as in big lecture theaters where attendance must be taken rapidly and efficiently.
Obtaining the GrFinger SDK
There are three editons of the GrFinger SDK: FREE, LIGHT, and FULL. From Griuale's web site:
There are three editons of the GrFinger SDK: FREE, LIGHT, and FULL. From Griuale's web site:
"The FREE Edition can be used for any non-commercial purposes and evaluation. Support is provided in forum.griaule.com. LIGHT Edition is intended for companies that would like to deploy GrFinger in many computers but does not need all of its speed. FULL edition unleashes all the GrFinger speed and allows you to access the fingerprint images and also features e-mail support."In addition, images captured by the FREE and LIGHT editions are encrypted, and for the FREE version, there is an advertisement banner displayed on the captured image. Consult Griaule's website for full pricing information for each edition. For this article, I have used the FULL edition (special thanks to Griaule for kindly granting me the license).
To obtain the GrFinger SDK, visit: http://griaule.com/page/en-us/downloads.
Creating the Application
Once the GrFinger SDK is installed, you are ready to create the application. For this article, I used Visual Studio .NET 2003. Create a new Windows application and name it C:\Fingerprintreader.
Once the GrFinger SDK is installed, you are ready to create the application. For this article, I used Visual Studio .NET 2003. Create a new Windows application and name it C:\Fingerprintreader.
First, you need to add the GrFingerxCtrl (an ActiveX control representing the GrFinger component) into your Toolbox. Right-click on Toolbox and select Add/Remove Items…. Check the GrFingerXCtrl Class (seeFigure 3) and click OK.
The GrFingerXCtrl control will now appear in the ToolBox (see Figure 4).
- PictureBox
- Label
- TextBox
- ListBox
- GroupBox
- Button
- AxGrFingerXCtrl
Figure 5. Populate the default Form1 with all the controls shown in the screen shot. |
Also, add an ImageList control and add an image to it (via the Images property). This image is the one shown in the PictureBox control as shown in Figure 5.
Coding the Application
The GrFinger SDK comes with a sample application written in different languages: VB6, Java, C++, VB.NET, etc. For the VB.NET version of this sample, there are two useful libraries that Griaule has provided: DBClass.vb and Util.vb. The DBClass.vb contains routines to add/retrieve users' information to/from a database. The Util.vb contains all the necessary routines to use the GrFingerXCtrl control and other supporting Win32 APIs. Rather than reinvent the wheel, I have decided to make use of them in my application. Hence, I will add the DBClass.vb and Util.vb files into my project (see Figure 6). Right-click on Solution Explorer and then select Add—>Existing Item…. Listings 1 and 2 show the full source of Util.vb and DBClass.vb. I will also modify the sample application provided by Griaule to suit the purpose of my application.
The GrFinger SDK comes with a sample application written in different languages: VB6, Java, C++, VB.NET, etc. For the VB.NET version of this sample, there are two useful libraries that Griaule has provided: DBClass.vb and Util.vb. The DBClass.vb contains routines to add/retrieve users' information to/from a database. The Util.vb contains all the necessary routines to use the GrFingerXCtrl control and other supporting Win32 APIs. Rather than reinvent the wheel, I have decided to make use of them in my application. Hence, I will add the DBClass.vb and Util.vb files into my project (see Figure 6). Right-click on Solution Explorer and then select Add—>Existing Item…. Listings 1 and 2 show the full source of Util.vb and DBClass.vb. I will also modify the sample application provided by Griaule to suit the purpose of my application.
Save the database GrFingerSample.mdb in C:\Fingerprintreader\bin\.
Let's now switch to the code-behind of Form1 and write the code to wire up all the controls.
First, import the GrFingerXLib namespace:
Imports GrFingerXLib
Declare the following constants and member variables:
'---name of the database---
Const DBFile = "GrFingerSample.mdb"
Const ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
'---for an instance of the Util.vb class---
Private myUtil As Util
'---for storing user's ID---
Private _UserID As Integer
'---database connection string---
Private connection As System.Data.OleDb.OleDbConnection
In the Form1_Load event, code the following:
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim err As Integer
' initialize util class
myUtil = New Util(ListBox1, PictureBox1, AxGrFingerXCtrl1)
' Initialize GrFingerX Library
err = myUtil.InitializeGrFinger()
' Print result in log
If err < 0 Then
myUtil.WriteError(err)
Exit Sub
Else
myUtil.WriteLog( _
"**GrFingerX Initialized Successfull**")
End If
'---create a log file---
If Not System.IO.File.Exists(Logfile) Then
System.IO.File.Create(Logfile)
End If
End Sub
Here, you create an instance of the Util class. The constructor of the Util class takes in three arguments: the ListBox control to display the status of the GrFingerXCtrl control, a PictureBox control to display the captured fingerprint image, and finally the GrFingerXCtrl control itself. You then initialize the GrFingerX library and create a log file (if it is not already present). This log file is a comma-separated value (CSV) file that contains users' IDs and login times.Next, service all the necessary events of the GrFingerXCtrl control:
' -----------------------------------------------------------------------------------
' GrFingerX events
' -----------------------------------------------------------------------------------
' A fingerprint reader was plugged on system
Private Sub AxGrFingerXCtrl1_SensorPlug( _
ByVal sender As System.Object, _
ByVal e As AxGrFingerXLib._IGrFingerXCtrlEvents_SensorPlugEvent) _
Handles AxGrFingerXCtrl1.SensorPlug
myUtil.WriteLog("Sensor: " & e.idSensor & ". Event: Plugged.")
AxGrFingerXCtrl1.CapStartCapture(e.idSensor)
End Sub
' A fingerprint reader was unplugged from system
Private Sub AxGrFingerXCtrl1_SensorUnplug( _
ByVal sender As System.Object, _
ByVal e As AxGrFingerXLib._IGrFingerXCtrlEvents_SensorUnplugEvent) _
Handles AxGrFingerXCtrl1.SensorUnplug
myUtil.WriteLog("Sensor: " & e.idSensor & ". Event: Unplugged.")
AxGrFingerXCtrl1.CapStopCapture(e.idSensor)
End Sub
' A finger was placed on reader
Private Sub AxGrFingerXCtrl1_FingerDown( _
ByVal sender As System.Object, _
ByVal e As AxGrFingerXLib._IGrFingerXCtrlEvents_FingerDownEvent) _
Handles AxGrFingerXCtrl1.FingerDown
myUtil.WriteLog("Sensor: " & e.idSensor & ". Event: Finger Placed.")
End Sub
' A finger was removed from reader
Private Sub AxGrFingerXCtrl1_FingerUp( _
ByVal sender As System.Object, _
ByVal e As AxGrFingerXLib._IGrFingerXCtrlEvents_FingerUpEvent) _
Handles AxGrFingerXCtrl1.FingerUp
myUtil.WriteLog("Sensor: " & e.idSensor & ". Event: Finger removed.")
End Sub
These events are raised when:- A fingerprint reader is plugged or unplugged from the computer
- A finger is placed or removed from the fingerprint reader
' An image was acquired from reader
Private Sub AxGrFingerXCtrl1_ImageAcquired( _
ByVal sender As System.Object, _
ByVal e As AxGrFingerXLib._IGrFingerXCtrlEvents_ImageAcquiredEvent) _
Handles AxGrFingerXCtrl1.ImageAcquired
' Copying aquired image
myUtil.raw.height = e.height
myUtil.raw.width = e.width
myUtil.raw.res = e.res
myUtil.raw.img = e.rawImage
' Signaling that an Image Event occurred.
myUtil.WriteLog("Sensor: " & e.idSensor & ". Event: Image captured.")
' display fingerprint image
myUtil.PrintBiometricDisplay(False, GRConstants.GR_DEFAULT_CONTEXT)
'---extract the template from the fingerprint scanned---
ExtractTemplate()
'---identify who the user is---
_UserID = IdentifyFingerprint()
If _UserID > 0 Then
'---user found---
Beep()
btnRegister.Enabled = False
'---display user's information---
GetUserInfo()
'---writes to log file---
WriteToLog(_UserID)
Else
'---user not found---
ClearDisplay()
btnRegister.Enabled = True
Beep()
lblMessage.Text = "User not found! Please register your information below"
End If
End Sub
Once the user's identity is found, you will display the user's particulars by calling the GetUserInfo() subroutine. You will also write an entry to the log file by calling the WriteToLog() subroutine.Once a fingerprint image is captured, you need to extract some characteristic points from the image; these are called minutiae. One regular fingerprint has approximately 50 minutiae. To identify a user, you need about 13 of them. When all the minutiae are extracted, they are put together into a structure called a Template, which is the joining of all the extracted minutiae in a fingerprint. According to Griaule, the identification is made by a triangulation process and geometrical relation between the minutiae only, not the entire image. This process is accomplished by the ExtractTemplate() function:
' Extract a template from a fingerprint image
Private Function ExtractTemplate() As Integer
Dim ret As Integer
' extract template
ret = myUtil.ExtractTemplate()
' write template quality to log
If ret = GRConstants.GR_BAD_QUALITY Then
myUtil.WriteLog("Template extracted successfully. Bad quality.")
ElseIf ret = GRConstants.GR_MEDIUM_QUALITY Then
myUtil.WriteLog("Template extracted successfully. Medium quality.")
ElseIf ret = GRConstants.GR_HIGH_QUALITY Then
myUtil.WriteLog("Template extracted successfully. High quality.")
End If
If ret >= 0 Then
' if no error, display minutiae/segments/directions into the image
myUtil.PrintBiometricDisplay(True, GRConstants.GR_NO_CONTEXT)
Else
' write error to log
myUtil.WriteError(ret)
End If
Return ret
End Function
The IdentifyFingerprint() function locates the identity of the user by calling the Identify() method located in the Util.vb class. It returns the ID of the identified user:
'---Identify a fingerprint; returns the ID of the user---
Private Function IdentifyFingerprint() As Integer
Dim ret As Integer, score As Integer
score = 0
' identify it
ret = myUtil.Identify(score)
' write result to log
If ret > 0 Then
myUtil.WriteLog("Fingerprint identified. ID = " & ret & ". Score = " & score & ".")
myUtil.PrintBiometricDisplay(True, GRConstants.GR_DEFAULT_CONTEXT)
ElseIf ret = 0 Then
myUtil.WriteLog("Fingerprint not Found.")
Else
myUtil.WriteError(ret)
End If
Return ret
End Function
The GetUserInfo() subroutine retrieves the user's particulars using the value of the _UserID variable:
'---get user's information---
Public Sub GetUserInfo()
Dim filePath As String
Try
filePath = Application.StartupPath() & "\" & DBFile
connection = New OleDb.OleDbConnection(ConnectionString & filePath)
connection.Open()
Dim reader As OleDb.OleDbDataReader
Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand
command.Connection = connection
'---retrieve user's particulars---
command.CommandText = "SELECT * FROM Enroll WHERE ID=" & _UserID
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
reader.Read()
'---display user's particulars---
lblMessage.Text = "Welcome, " & reader("name")
txtSSN.Text = reader("SSN")
txtName.Text = reader("Name")
txtCompany.Text = reader("Company")
txtContactNumber.Text = reader("ContactNumber")
txtEmail.Text = reader("Email")
'---reset the timer to another 5 seconds---
Timer1.Enabled = False
Timer1.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
When a new user scans his fingerprint for the first time, naturally, thefingerprint is not recognized. The user can then register the fingerprint by filling his identifying data, in this case the SSN, name, company name, contact number, and email address. This is accomplished using the Register button, which first adds the fingerprint to the database (via the EnrollFingerprint() function) and then adds the particulars of the user using the AddNewUser() subroutine:
'---Register button---
Private Sub btnRegister_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnRegister.Click
'---first add the fingerprint---
_UserID = EnrollFingerprint()
'---then add the particulars---
AddNewUser()
'---clears the display---
ClearDisplay()
'---writes to log file---
WriteToLog(_UserID)
End Sub
The EnrollFingerprint() function enrolls a fingerprint into the database using the Enroll() method defined in the Util.vb class:
'---adds a fingerprint to the database; returns the ID of the user---
Private Function EnrollFingerprint() As Integer
Dim id As Integer
' add fingerprint
id = myUtil.Enroll()
' write result to log
If id >= 0 Then
myUtil.WriteLog("Fingerprint enrolled with id = " & id)
Else
myUtil.WriteLog("Error: Fingerprint not enrolled")
End If
Return id
End Function
The AddNewUser() subroutine saves the user's particulars into the database:
'---Add a new user's information to the database---
Public Sub AddNewUser()
Dim filePath As String
Try
filePath = Application.StartupPath() & "\" & DBFile
connection = New OleDb.OleDbConnection(ConnectionString & filePath)
connection.Open()
Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand
command.Connection = connection
'---set the user's particulars in the table---
Dim sql As String = "UPDATE enroll SET SSN='" & txtSSN.Text & "', " & _
"Name='" & txtName.Text & "', " & _
"Company='" & txtCompany.Text & "', " & _
"ContactNumber='" & txtContactNumber.Text & "', " & _
"Email='" & txtEmail.Text & "' " & _
" WHERE ID=" & _UserID
command.CommandText = sql
command.ExecuteNonQuery()
MsgBox("User added successfully!")
connection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
The ClearDisplay() subroutine clears away the information displayed in the various TextBox controls:
'---Clears the user's particulars---
Public Sub ClearDisplay()
lblMessage.Text = _
"Please place your index finger on the fingerprint reader"
PictureBox1.Image = ImageList1.Images(0)
txtSSN.Text = String.Empty
txtName.Text = String.Empty
txtCompany.Text = String.Empty
txtContactNumber.Text = String.Empty
txtEmail.Text = String.Empty
End Sub
When the Timer1_Tick event is fired (every 5 seconds), call the ClearDisplay() subroutine to clear the display:
'---the Timer control---
Private Sub Timer1_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Timer1.Tick
ClearDisplay()
Timer1.Enabled = False
End Sub
The WriteToLog() subroutine writes to the log file an entry containing user's ID and the current time:
Public Sub WriteToLog(ByVal ID As String)
'---write to a log file---
Dim sw As New System.IO.StreamWriter( _
Logfile, True, System.Text.Encoding.ASCII)
sw.WriteLine(id & "," & Now.ToString)
sw.Close()
End Sub
Testing the Application
You are now ready to test the application. Press F5 in Visual Studio .NET 2003 and you will see the application as shown in Figure 8
Place your index finger on the fingerprint reader and you should be prompted to register with your particulars (personal data). Once registered, your particulars will be cleared after five seconds. Once that's completed, you can try placing the same finger to check if you can be identified correctly. If so, you will see your information displayed as in Figure 9.
Note that there will be times where your fingerprint may not be correctly identified. This is likely due to incorrect positioning of your finger. Try again and it should be identified correctly.
Applications for Biometrics Abound
In this article, you have seen how you can integrate the fingerprint reader into your .NET application. While the example shown in this article is very simple, you can easily extend it to more complex scenarios, such as video rental applications, payment services, etc. If you have not started evaluating biometric authentication for your projects, this is a very good time to start..
Download the Code for this Article