Authentication by Usb Flash Driver
This article is another method for authentication. General method is simple username and password required authentication. This method may be additional your auth. method. Authorization is using for a role based security.
Required Equipments:
Another usb flash driver. (It’s damaged or secured, it can saw a “my computer” only.)
Analysis
We are get the usb flash drive PNPDeviceID, if my drive is needed drive than condition has true else false. Checking usb flash drive is another advantage for user: User use a another computer. (Same Rooming Profile for Server Operating System).
WMI (Windows Managment Interface) : WMI has created windows 2000 and later windows os. (Win NT Based OS). It’s get more information on running system and checking and managing litle system object. Any query with WQL for WMI. Windows Xp has approximate 900 WMI object. If you see wmi tester then click start menu and click run, write wbemtest and click Ok. For more information click here.
Example: “SELECT * From Win32_LogicalFileAccess”, “SELECT * From Win32_LoggedOnUser” … like sql query:)
Writing Code
Create a new console project in visual studio 2005/2008
Open program.cs and paste following method
private static bool ScanNeededDiskDriver(string PNPDeviceID)
{
Console.WriteLine("Starting query");
ManagementObjectSearcher mOSClass = new ManagementObjectSearcher();
mOSClass.Query = new ObjectQuery("SELECT * From Win32_DiskDrive");
Console.WriteLine("\tSarching disk driver.");
foreach (ManagementObject mOClass in mOSClass.Get())
{
if (mOClass["PNPDeviceID"].ToString() == PNPDeviceID)
{
Console.WriteLine("\t\tSucces. Needed driver was found.");
return true;
}
}
Console.WriteLine("\t\tNeeded disk driver can not be found.\r\n\t\tPlease insert another driver.");
return false;
}
and paste following code in the main method
if (ScanNeededDiskDriver(@"USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0"))
{
// to do ...
}
else
{
// to do ...
}
If your needed driver has been plugged then method is return true else false. For more information ManagementObjectSearcher Class
Full Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace Usb
{
public class Program
{
[STAThread]
static void Main(string[] args)
{
if (ScanNeededDiskDriver(@"USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0"))
{
// to do ...
}
else
{
// to do ...
}
}
private static bool ScanNeededDiskDriver(string PNPDeviceID)
{
Console.WriteLine("Starting query");
ManagementObjectSearcher mOSClass = new ManagementObjectSearcher();
mOSClass.Query = new ObjectQuery("SELECT * From Win32_DiskDrive");
Console.WriteLine("\tSarching disk driver.");
foreach (ManagementObject mOClass in mOSClass.Get())
{
if (mOClass["PNPDeviceID"].ToString() == PNPDeviceID)
{
Console.WriteLine("\t\tSucces. Needed driver was found.");
return true;
}
}
Console.WriteLine("\t\tNeeded disk driver can not be found.\r\n\t\tPlease insert another driver.");
return false;
}
}
}
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management
Namespace Usb
Public Class Program
_
Private Shared Sub Main(ByVal args As String())
' to do ...
If ScanNeededDiskDriver("USBSTOR\DISK&VEN_SONY&PROD_STORAGE_MEDIA&REV_1.00\A304050400186&0") Then
' to do ...
Else
End If
End Sub
Private Shared Function ScanNeededDiskDriver(ByVal PNPDeviceID As String) As Boolean
Console.WriteLine("Starting query")
Dim mOSClass As New ManagementObjectSearcher()
mOSClass.Query = New ObjectQuery("SELECT * From Win32_DiskDrive")
Console.WriteLine("" & Chr(9) & "Sarching disk driver.")
For Each mOClass As ManagementObject In mOSClass.[Get]()
If mOClass("PNPDeviceID").ToString() = PNPDeviceID Then
Console.WriteLine("" & Chr(9) & "" & Chr(9) & "Succes. Needed driver was found.")
Return True
End If
Next
Console.WriteLine("" & Chr(9) & "" & Chr(9) & "Needed disk driver can not be found." & Chr(13) & "" & Chr(10) & "" & Chr(9) & "" & Chr(9) & "Please insert another driver.")
Return False
End Function
End Class
End Namespace
If ManagementObjectSearcher class can not be found then please add System.Managament.dll reference.
Have fun.
Leave a comment