Search Tools Links Login

LDAP Password Authentication

Show how to use LDAP to authenticate users

Original Author: linda samson


LDAP Server, LDAP Port, UID, Organization, Organizational Unit, Password


Requires Microsoft ActiveX Data Objects 2.0/2.1/2.5 Library
This is just a code snippet. You must supply the command button, etc.


True - if authenticated, False - if not


'Requires Microsoft ActiveX Data Objects 2.0/2.1/2.5 Library (Project | References)
'Private Sub Command1_Click()
'  Init()
'  msgbox Authenticate
'End Sub
dim m_LDAPServer as string
dim m_LDAPPort as string
dim m_Org as string
dim m_OrgUnit as string
dim m_Initial as string
dim m_Password as string
Public Sub Init()
  m_LDAPServer = ""
  m_LDAPPort  = "389"
  m_Org    = "" 'o=
  m_OrgUnit  = "People"       'ou=
  m_Initial  = "userId"       'uid=
  m_Password  = "password"
End Sub
Public Function Authenticate() As Boolean
On Error Resume Next
Dim con As New Connection
Dim sqlStmt As String
Dim connStr As String
Dim rs As Recordset
  'TODO: trap blank UID and password here!
  If (m_Initial = "") then
    Msgbox "No Initial"
    Exit Function
  End If
  if (m_Password = "") then
    Msgbox "No Password"
    Exit Function
  End If
  'prepare SQL statement
  sqlStmt = "SELECT uid " & _
     "FROM 'LDAP://" & m_LDAPServer & ":" & m_LDAPPort & "/o=" & m_Org & "/ou=" & m_OrgUnit & "' " & _
     "WHERE uid='" & m_Initial & "'" & " and objectClass='*'"
  'create Active Directory Service Object
  Set con = CreateObject("adodb.connection")
  con.Provider = "ADSDSOOBject"
  'construct connection string
  connStr = "uid=" & m_Initial & ",ou=" & m_OrgUnit & ",o=" & m_Org
  'open connection + password
  con.Open "ADs Provider", connStr, m_Password
  'execute our query
  Set rs = con.Execute(sqlStmt)
  'rs will be empty if authentication fails
  Authenticate = Not (IsEmpty(rs) Or (Err.Number = -2147217911))
  'need to close
End Function

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 102 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.