Thursday, January 26, 2012

VB6 Authenticate user against AD

Now I know VB6 is old, but it still works and is easy to quickly write an app, well I thought so until this morning when I needed a simple app that can verify a username and password on my domain. In PHP it is a simple ldap query and the user is verified, trying the same thing in VB6, no chance, it works fine if you are logged on to a domain machine, or there is a way if you are logged on locally to a domain machine, but if your machine is not part of the domain it is a pain in the rear.

After trying for an hour, reading many pages on the web, I finally gave up and went to do something simple (clear the filter in a projector.) Then an idea hit me which works and is very simple.

It goes as follows:
1) find the address of any share on the domain which all users have read access to.
2) In VB6 map the share using supplied credentials.
3) If successful, Congrats the user is verified! unmap the share.
4) If not successful deal with the error.


Code as follows:

Dim objnetwork As Object
Set objnetwork = CreateObject("WScript.Network")
On Error Resume Next
objnetwork.MapNetworkDrive "H:", "\\server\share", "false", username.text, password.text
objnetwork.RemoveNetworkDrive "H:"
If Err <> 0 Then
msgbox = "Invalid Username or Password"
'code here for successful logon
End If

End of Code

That is it, no extra declarations no modules, no ldap naming convention, just simple code easy to use with 2 text boxes and a command button.

No comments: