VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



LDAP Password Authentication

by linda samson (7 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

Show how to use LDAP to authenticate users

Inputs
LDAP Server, LDAP Port, UID, Organization, Organizational Unit, Password
Assumes
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.
Code Returns
True - if authenticated, False - if not

Rate LDAP Password Authentication

'linda
'Requires Microsoft ActiveX Data Objects 2.0/2.1/2.5 Library (Project | References)
'TODO: 
'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 = "ldapserver.com"
  m_LDAPPort  = "389"
  m_Org    = "ldaporg.com" '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
  rs.Close
End Function

Download this snippet    Add to My Saved Code

LDAP Password Authentication Comments

No comments have been posted about LDAP Password Authentication. Why not be the first to post a comment about LDAP Password Authentication.

Post your comment

Subject:
Message:
0/1000 characters