我最近接管了一些使用LDAP查询AD的Delphi代码(这是Delphi 5,但我认为这不重要)。代码非常混乱,因为它包含ConnectionString和直接设置属性。有一个安装程序,输入了许多AD参数。代码的哪些部分是相关的,哪些部分可以切割?如果安装程序中存在不必要的问题,我特别感兴趣。
Data Source
(在安装程序中作为ADserver给出)或Location
(在安装程序中作为ADlocation给出)?我尝试删除aADOconnection.provider:='ADsDSOObject';
或ConnectionString,并且调用有效。当我删除两个用于设置提供程序,和 ConnectionString的代码时,调用当然失败了。此代码段用于检查AD连接,有一个类似(但更长)的代码块,用于查询用户的组。
Connecstr_S:='Provider=ADsDSOObject;Encrypt Password=False;Data Source='+
Tsystem.programsettings.sADserver+';Location='+
Tsystem.programsettings.sADlocation+';Mode=Read;Bind Flags=0;ADSI Flag=-2147483648';
aADOconnection:=TADOConnection.create(nil);
aADOcmd:=TADOCommand.create(nil);
aADOconnection.loginprompt:=false;
aADOconnection.commandtimeout:=30;
aADOconnection.connectoptions:=coConnectUnspecified;
aADOconnection.CursorLocation:=cluseclient;
aADOconnection.isolationlevel:=ilCursorStability;
aADOconnection.mode:=cmunknown;
aADOconnection.provider:='ADsDSOObject';
aADOcmd.commandtype:=cmdUnknown;
aADOcmd.Paramcheck:=false;
aADOcmd.commandtimeout:=30;
aADOcmd.Prepared:=false;
aADOconnection.ConnectionString:=Connecstr_S;
aADOcmd.Connection:=aADOconnection;
aADOconnection.open;
result:=true;
try
SQL:='select displayname from '+quotedstr('LDAP://'+Tsystem.programsettings.sADbaseDN)+
' where '+Tsystem.programsettings.sADuseridAttribute+'='+quotedstr('aaaaaaa');
aADOcmd.CommandText:=SQL;
t:=aADOcmd.Execute;
except
On E:EOleException do result:=false; //network connection probably missing
end;
aADOconnection.close;
aADOcmd.free;
aADOconnection.free;