开发者

How to connect to an OpenLDAP server using ADO (or else) and Delphi

I'm trying to connect to an OpenLDAP server 'clone'. I've tried using Synapse Library but I was able to get only a part (about 50%) of our public contacts.

I'm trying now the ADO way (I've read that ADSI was compatible with other LDAP servers) but I can't get it working.

ADOConnection provider connection string looks like this :

Provider=ADsDSOObject;Encrypt Password=False;Integrated Security=SSPI;Data Source=NIS;Mode=Read;Bind Flags=0;ADSI Flag=-2147483648;

ADOConnection.LoginPrompt is set to true.

ADOQuery SQL Statement looks like this :

Select Description FROM 'LDAP://192.168.xxx.xxx/fn=Public Folders/cn=user@domain.com/fn=ContactRoot' WHERE objectClass='*'
开发者_JS百科

I'm getting an error when opening the ADOQuery (translated from French) : "An non valid directory path was sent"

What is wrong here ? Is there any other free solution than ADO / Synapse ?

Thank you in advance

SW


You can use COM:

  • Generate the follwoing unit (ActiveDs_TLB.pas).

Now you can:

  • Get the unit adshlp.pas from http://www.agnisoft.com.

or

Define the following:

function ADsGetObject( lpszPathName: WideString; const riid: TGUID; out ppObject ):HRESULT; stdcall; external 'activeds.dll';

you can get LDAP objects using code like:

function GetUserNameFromAD( const ADName: string ): string;
var
    Token:              THandle;
    Info, User, Domain: array [ 0..255 ] of Byte;
    ILen, ULen, DLen:   Longword;
    Use:                SID_NAME_USE;
    Usr:                IAdsUser;
begin
    Result := '';
    // Get the thread token. If the current thread is not impersonating, get the process token.
    if not OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, True, Token ) then begin
        if GetLastError() <> ERROR_NO_TOKEN then Exit;
        if not OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, Token ) then Exit;
    end;
    try
        // Get name of domain and username using the token.
        if not GetTokenInformation( Token, TokenUser, @Info, sizeof( Info ), ILen ) then Exit;
        ULen := sizeof( User );
        DLen := sizeof( Domain );
        if not LookupAccountSid( nil, PSIDAndAttributes( @Info )^.Sid, @User, ULen, @Domain, DLen, Use ) then Exit;
        // Should be the specified domain
        if ADName <> PChar( @Domain ) then Exit;
        // Check user and domain with the AD and obtain the username from the AD
        if ADsGetObject( 'WinNT://' + PChar( @Domain ) + '/' + PChar( @User ), IADsUser, Usr ) <> S_OK then Exit;
        if Assigned( Usr ) then Result := Usr.Name;
    finally
        CloseHandle(Token);
    end;
end;

There are some examples on the web how to use these units.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜