LDAP query with FILTER
Suppose I have t开发者_运维问答he following LDAP query:
Base DN: OU=Groups,DC=office,DC=domain,DC=org
Filter: (member:1.2.840.113556.1.4.1941:=CN=adam smith,OU=Users,DC=office,DC=domain,DC=org)
How can I execute it under Delphi(2007)? Examples using ADO seem to have SQL'ish syntax and I do not now how to convert it?
In Delphi, you can use two ways of getting at your data:
either the "SQL'ish" syntax you describe - basically ADO access to Active Directory. That's easy, if you have a SQL background, but it's also limited in some ways (e.g. you cannot get at multi-valued attributes and such). You'll find some Search Tips on ADO on Richard Mueller's site (AD Programming MVP)
import the ActiveDs.tlb type library and use the COM interfaces (most notably
IDirectorySearch
) provided by ADSI to search. It's a rather messy COM interface, that's probably why most tend to use the ADO search stuff which is more readily approachable
Way back when I was still programming Delphi, I did a lot of Active Directory stuff and puts some of my Delphi / AD tips and some sample code onto my site. It's not been updated in quite a while though :-( But the ADSISearch component might be of interest to you (and other Delphites)
Update: can you try this "SQL-ish" statement in your TADOCommand??
SELECT sAMAccountName, displayName
FROM 'LDAP://OU=Groups,DC=office,DC=domain,DC=org'
WHERE objectCategory='group'
AND member:1.2.840.113556.1.4.1941:=(CN=adam smith,OU=Users,DC=office,DC=domain,DC=org)
Desicion for your question:
var ADOConnection, ADOCmd, Res: Variant;
ADOConnection := CreateOleObject('ADODB.Connection');
ADOCmd := CreateOleObject('ADODB.Command');
try
ADOConnection.Provider := 'ADsDSOObject';
ADOConnection.Open('Active Directory Provider');
ADOCmd.ActiveConnection := ADOConnection;
ADOCmd.Properties('Page Size') := 100;
ADOCmd.Properties('Timeout') := 30;
ADOCmd.Properties('Cache Results') := False;
sBase := '<GC://' + sADForestName+ '>';
sFilter := '(&(objectCategory=person)(objectClass=user)' +
'(distinguishedName=' + sADUserName + ')' +
'(memberOf:1.2.840.113556.1.4.1941:=' + sADGroupName + '))';
sAttributes := 'sAMAccountName';
ADOCmd.CommandText := sBase + ';' + sFilter + ';' + sAttributes + ';subtree';
Res := AdoCmd.Execute;
if Res.EOF then User := ''
else User := Res.Fields[0].Value;
finally
ADOCmd := NULL;
ADOConnection.Close;
ADOConnection := NULL;
end;
精彩评论