-
Daniel Seguin
- PowerBuilder
- Tuesday, 24 March 2026 07:54 PM UTC
Hi guys,
It's my first time trying to connect to ldap to authenticate users with active directory
Here is my poc source code:
I am having trouble to create and execute the query to check username, can someone help.
// Password is valid if we got here. Now validate user meets filter criteria
OLEObject lo_cmd, lo_rs
string ls_sql
string ls_domain_fqdn
string ls_port
string ls_dc_components
string ls_username
OLEObject lo_conn
boolean lb_user_valid
string ls_from
ls_username = gf_getenv("USERNAME")
ls_domain_fqdn = "SSL-PSPC.pwgsc-tpsgc.gc.ca"
ls_port = "636"
ls_dc_components = "DC=ad,DC=pwgsc-tpsgc,DC=gc,DC=ca"
lo_cmd = CREATE OLEObject
lo_cmd.ConnectToNewObject("ADODB.Command")
lo_cmd.ActiveConnection = lo_conn
lo_cmd.CommandType = 1 // adCmdText
// --------------------------------------------------------------------
// SQL-style LDAP query
// --------------------------------------------------------------------
// Valid LDAPS://SSL-PSPC.pwgsc-tpsgc.gc.ca:636/DC=ad,DC=pwgsc-tpsgc,DC=gc,DC=ca
ls_from = "FROM 'LDAP://" + ls_domain_fqdn + ":" + ls_port + "/" + ls_dc_components + "' "
ls_sql = "SELECT sAMAccountName, mailNickname, userPrincipalName " + trim(ls_from) + " WHERE sAMAccountName='" + ls_username + "'"
/*
ls_sql = "SELECT sAMAccountName, displayName, mail " + &
"FROM 'LDAP://" + ls_domain_fqdn + ":" + ls_port + "/" + ls_dc_components + "' " + &
"WHERE objectCategory = 'person' " + &
"AND objectClass = 'user' " + &
"AND sAMAccountName = '" + ls_username + "' " + &
"AND NOT distinguishedName LIKE '%OU=NON-PERSON,%' " + &
"AND NOT distinguishedName LIKE '%OU=TestUsers,%' " + &
"AND sAMAccountName NOT LIKE 'test0%' " + &
"AND sAMAccountName NOT LIKE 'test1%' " + &
"AND sAMAccountName NOT LIKE 'eevtest%'"
*/
// Debug (optional but useful)
MessageBox("LDAP SQL QUERY", ls_sql)
lo_cmd.CommandText = ls_sql
try
// Execute query
lo_rs = lo_cmd.Execute()
catch (Exception ex)
MessageBox("Error", ex.GetMessage() )
return
end try
// Check if user was found
IF NOT lo_rs.EOF THEN
lb_user_valid = TRUE
string ls_display_name, ls_email
ls_display_name = ""
ls_email = ""
IF NOT IsNull(lo_rs.Fields("displayName").Value) THEN
ls_display_name = lo_rs.Fields("displayName").Value
END IF
IF NOT IsNull(lo_rs.Fields("mail").Value) THEN
ls_email = lo_rs.Fields("mail").Value
END IF
MessageBox("Login Successful", &
"Authentication successful!~r~n~r~n" + &
"User: " + ls_username + "~r~n" + &
"Display Name: " + ls_display_name + "~r~n" + &
"Email: " + ls_email)
ELSE
MessageBox("Login Failed", &
"User '" + ls_username + "' does not meet validation criteria.~r~n" + &
"Account may be a test or service account.")
END IF
lo_rs.Close()
DESTROY lo_rs
DESTROY lo_cmd
Find Questions by Tag
Helpful?
If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.