使用Delphi通过LDAP与user@mydomain.com进行Active Directory身份验证

时间:2014-11-25 19:44:36

标签: delphi active-directory ldap

正如您从下面的代码段中看到的那样。我目前正在使用adshlp和ActiveDs_TLB从当前登录的用户收集有关AD的信息。我有一个表单,允许用户输入他们的AD密码,我在允许访问系统之前验证是否正确。这很好吃。我现在遇到的问题是用户希望能够以mydomain.com \ userid的形式输入任何AD和ID,并让代码进行身份验证并带回代码当前检索的相同数据。我无法找到可以执行此操作的LDAP调用。我将不胜感激任何帮助和建议。 谢谢



uses
adshlp, ActiveDs_TLB


function Tlogon_form.GetUser(Domain, UserName, pword: string; var ADSIUser: TADSIUserInfo): boolean;
var
  usr   :    IAdsUser;
  usr1  :    IADs;
  flags :    integer;
  grps  :    IAdsMembers;
  grp   :    IAdsGroup;
  varGroup : OleVariant;
  Temp :     LongWord;
  pwd, cn_name, FQDN, AD_path: string;
  HR : boolean;
  fad_domain:string;
  objsysinfo: IADsADSystemInfo;
  domainDN: string;
  List: array [0..10] of String;
  I: integer;
  name_nodes :string;

const
  ADS_SECURE_AUTHENTICATION = $00000001;
begin
  ADSIUser.UID:='';
  ADSIUser.UserName:='';
  ADSIUser.DB_login :='';
  ADSIUser.Disabled:=true;
  ADSIUser.LockedOut:=true;
  ADSIUser.Groups:='';
  Result:=false;
  FQDN :='';
  AD_path := '';
  SBN_SQL.Common_login :='';

  FPassword := pword;
  FUserName := UserName;
  //FDomain := lowercase(Domain); // + '.local';

  if FUserName = '' then exit;

  objsysinfo := CoADSystemInfo.Create;
  domainDN := objsysinfo.GetAnyDCName;
  fad_domain := objsysinfo.DomainDNSName;
  name_nodes := objsysinfo.UserName;

  if domain > '' then
  begin
    fad_domain := domain;
  end
  else
  begin
    domain := fad_domain;
  end;

  fad_domain := fad_domain + '.';

  FQDN := domainDN;
  ad_path := name_nodes;

    try
     if trim(FUserName)<>'' then
     begin
        ADsOpenObject('LDAP://' + AD_path, FUserName, FPassword,ADS_SECURE_AUTHENTICATION, IADsUser, usr);
     end;

     if usr=nil then exit;

     ADSIUser.UID:= UserName;

     ADSIUser.UserName := usr.FullName;
     ADSIUser.DB_login := usr.employeeid;
     //usr:=nil;
     Result:=true;
     exit;
  except
     on e: exception do begin
        Result:=false;
        exit;
     end;
  end;


end;
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

可以做的是根据userid(没有域名)搜索该用户,从而获取相关信息。

我在2000年10月在“The Delphi Magazine”中写了一篇关于使用ADSI和Delphi进行搜索的文章 - 你仍然可以下载我的code sample and a Delphi component TADSISearcher from my web site - 希望这可以帮助你入门!

答案 1 :(得分:1)

我还使用 ADsOpenObject 进行LDAP验证,并在您的代码中将域作为参数传递,因此在 ADsOpenObject 调用中使用此参数或者我可能没有清楚理解这个问题

function Authenticate(const pUser, pPassword,pDomain: String): HRESULT;  
Var  
 aUser : IAdsUser;  
begin  
 Try  
   Result  := ADsOpenObject(Format('LDAP://%s',[pDomain]),Format('%s\%s',[pDomain,pUser]),pPassword,ADS_SECURE_AUTHENTICATION,IAdsUser,aUser);    
  // here retrieve the information needed   
 Finally  
   aUser := Nil  
 End  
end;