Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use Active Directory Authentication in Visual Basic (6.0)

5,921 views
Skip to first unread message

PONG

unread,
Feb 15, 2007, 10:43:02 PM2/15/07
to
Hi there,

I'd like to learn HOW TO CHECK AUTHENTICATION OF USER IN VISUAL BASIC, it
would be great if anyone can tell me the syntax of query string.

Following codes is success when I use it in ASP:

'***************************************************************************

Sub AuthCheck(ByVal HavingInput)
Dim RootDSE
Dim DSObject
Dim Auth
Dim NamingContext
Const ADS_SECURE_AUTHENTICATION=1

Set RootDSE = GetObject("GC://rootDSE")
NamingContext = RootDSE.Get("defaultNamingContext")

Set DSObject = GetObject("GC:")

Set Auth = DSObject.OpenDSObject("GC://" & NamingContext, sUser, sPassword,
ADS_SECURE_AUTHENTICATION)

If err = 0 then
Response.Redirect("grant.asp")
Else
Response.Redirect("fail.asp")
End If

Set Auth = Nothing
Set DSObject = Nothing
Set RootDSE = Nothing
End Sub

'***************************************************************************

I modified wonder what should be the query string "strQuery" in the
following Visual Basic Codes:

'***************************************************************************

Public Function UserInfo(LoginName As String) As String
Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot, oDomain As IADs
Dim strBase, strFilter, strDomain As String
Dim strAttribs, strDepth, strQuery As String
Dim strQuery, strAns As String
Dim user As IADsUser

On Error GoTo ErrUserInfo:

Set oRoot = GetObject("GC://intern.company.com/rootDSE")
strDomain = oRoot.Get("defaultNamingContext")

Set oDomain = GetObject("GC:")

strBase = "GC://intern.company.com"
strBase = strBase & "/" & strDomain

strAttribs = "adsPath"
strDepth = "subTree"
strFilter = "User ID=testacc"

strQuery = strBase & "," & strFilter & "," & strAttribs & "," & sDepth &
",Encrypt Password= True,ADSI Flag= ADS_SECURE_AUTHENTICATION"

conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"

Set rs = conn.Execute(strQuery)

If Not rs.EOF Then
Set user = GetObject(rs("adsPath"))
With user

strAns = "First Name: " & .FirstName & vbCrLf
strAns = strAns & "Last Name " & .LastName & vbCrLf
strAns = strAns & "Email Address: " & .EmailAddress & vbCrLf
strAns = strAns & "Last Login: " & .LastLogin & vbCrLf
strAns = strAns & "Last Logoff: " & .LastLogoff & vbCrLf

End With
End If
UserInfo = strAns

ErrUserInfo:

If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If

If Not conn Is Nothing Then
If conn.State <> 0 Then conn.Close
Set conn = Nothing
End If

Set oRoot = Nothing
Set oDomain = Nothing
End Function

'***************************************************************************

Thanks all in advance,

Pong Wong

waw...@prcvap.microsoft.com

unread,
Feb 16, 2007, 1:33:22 AM2/16/07
to
Hi Pong,

Based on my understanding, you've already learned how to authenticate a user in ASP using ADSI and you want to know how to do that in VB. Another question is how to write a query to query a user's information.

Well, I think the script you're using in ASP should be still working fine in VB, although you will lose some strong type information.

For how to write the query to return a user's information, please see following two articles:

#Microsoft Windows 2000 Scripting Guide - Searching Active Directory for User Accounts
http://www.microsoft.com/technet/scriptcenter/guide/sas_usr_ykxh.mspx

#Query Active Directory for Information About a User
http://www.freevbcode.com/ShowCode.Asp?ID=710


Please feel free to let me know if there's anything unclear. Thanks.

Best regards,
Walter Wang
Microsoft Online Community Support

PONG

unread,
Feb 16, 2007, 4:35:15 AM2/16/07
to
Dear Walter,

Actually I'd like to bound the access of my application by the
authentication of AD, simply return True / False just like my ASP version.
However, there seem no "OpenDSObject" in VB, moreover, it return "Table does
not exist." after I modified the sample codes from
"http://www.freevbcode.com/ShowCode.Asp?ID=710".

Is there any function like "OpenDSObject" in VB?

How can I output the structure of the AD of a server by my WinXP PC for
further information?

Thanks for your reponse,

Pong Wong

PONG

unread,
Feb 26, 2007, 3:38:05 AM2/26/07
to
Happy Lunar New Year to all.

The main problem I faces is that the AD tree structure of the server seemed
not to be the same as the demo in
"http://www.freevbcode.com/ShowCode.Asp?ID=710".

i.e. the "strQuery" not valid for me to get the account information. It
returns "one or more error..."

Is there any way that I can output the tree structure of the server?

Thanks in advance,

Pong Wong

PONG

unread,
Feb 26, 2007, 5:00:15 AM2/26/07
to
Hi all,

How can I use the Authenticating Services in my VB6 application as it works
in ASP as follows:

"DSObject.OpenDSObject("GC://" & NamingContext, sUser, sPassword,
ADS_SECURE_AUTHENTICATION)"

THanks a lot,

Pong Wong

waw...@prcvap.microsoft.com

unread,
Mar 1, 2007, 3:18:28 AM3/1/07
to
Hi Pong,

Sorry for delayed reply.

You should still be able to use the code in your first message in VB6, for example, following code works on my side in a VB6 program:

Function AuthCheck(ByVal sUser As String, ByVal sPassword As String) As Boolean
On Error GoTo E

Dim RootDSE
Dim DSObject
Dim Auth
Dim NamingContext
Const ADS_SECURE_AUTHENTICATION = 1

Set RootDSE = GetObject("GC://rootDSE")
NamingContext = RootDSE.Get("defaultNamingContext")

Set DSObject = GetObject("GC:")

Set Auth = DSObject.OpenDSObject("GC://" & NamingContext, sUser, sPassword, ADS_SECURE_AUTHENTICATION)

Set Auth = Nothing
Set DSObject = Nothing
Set RootDSE = Nothing

AuthCheck = True
Exit Function
E:
AuthCheck = False
' log the err.Description if needed
End Function


Calling AuthCheck("<user>", "<password>") will return True if the user/password is correct and return False if not.

If you need to use strong-typed objects, you need to reference "Active DS Type Library" in your VB6 project and use those objects accordingly. For example, OpenDSObject is a method of the interface IADsOpenDSObject.

PONG

unread,
Mar 1, 2007, 3:38:05 AM3/1/07
to
Dear Walter,

Thanks for your reply and I'll try.

Pong Wong

PONG

unread,
Mar 2, 2007, 4:04:02 AM3/2/07
to
Dear Walter,

Yeah! That work! Thank you very much.

However, during debugging, I have another question:

"How can I output the structure of the AD of a server by my WinXP PC? (As I
don't know the structure even the name of the OUs)"

Thanks again.

Pong Wong

waw...@prcvap.microsoft.com

unread,
Mar 6, 2007, 1:20:53 AM3/6/07
to
Hi Pong,

I think you will need to learn the Active Directory Schema of your domain. The Active Directory Schema is like database schema, which describes the structure of your AD.

You could use ADSI Edit (http://www.computerperformance.co.uk/w2k3/utilities/adsi_edit.htm) to view the schema.

More resources:

http://www.awprofessional.com/articles/article.asp?p=26136&seqNum=3&rl=1



Best regards,
Walter Wang
Microsoft MSDN Online Support

PONG

unread,
Mar 6, 2007, 4:17:17 AM3/6/07
to
Thank you very much.

Usmani@discussions.microsoft.com Raza Usmani

unread,
Jan 14, 2009, 6:29:03 AM1/14/09
to
Hello, I am new to VB 6, can you please tell me what is RootDSE? and
DSObject? and how can i define them.
I just used this code into my project. I gave my local active directory name
at the place of rootDSE in getobject arguments. but when it reads the
instruction of Naming Context the system halts for seconds and then Error
Descripton msgbox appears saying that Active Directory properties are not
found in the Cache.

Need help.

regards

Raza Usmani

0 new messages