1

Windows 2008 サーバーで Active Directory サービスをセットアップしました。ユーザーを追加しました。DN (DistingushedName) は次のとおりです。CN=ashwin,CN=Users,DC=test,DC=com

DN にはパスワードが設定されておらず、匿名バインドが許可されています。AD に接続してユーザーを検索するサンプル (テスト コード) C++ プログラムがあります。

#include "windows.h"
#include "winldap.h"
#include "stdio.h"

//  Entry point for your application
int main(int argc, char* argv[])
{
    LDAP* pLdapConnection = NULL;
    INT returnCode = 0; 
    INT connectSuccess = 0;
    ULONG version = LDAP_VERSION3;
    LONG lv = 0;
    int option(0);
    LDAPMessage *vLdapMessage;

    //  Initialize an LDAP session without SSL.
    pLdapConnection = ldap_init("192.168.56.128",389);
    if (pLdapConnection == NULL)
    {
        printf( "ldap_init failed with 0x%x.\n",hr);
        return -1;
    }

    //  Specify version 3; the default is version 2.
    returnCode = ldap_set_option(pLdapConnection,
        LDAP_OPT_PROTOCOL_VERSION,
        (void*)&version);
    if (returnCode != LDAP_SUCCESS)
        goto FatalExit;

    //Turning off referrals
    ldap_set_option(pLdapConnection, LDAP_OPT_REFERRALS, LDAP_OPT_OFF); // required 

    //  Connect to the server.
    connectSuccess = ldap_connect(pLdapConnection, NULL);

    if(connectSuccess != LDAP_SUCCESS)
    {
        printf("ldap_connect failed with 0x%x.\n",connectSuccess);
        goto FatalExit;
    }

    //  Bind with current credentials. 
    printf("Binding ...\n");
    returnCode = ldap_bind_s(pLdapConnection,NULL, NULL, LDAP_AUTH_SIMPLE);
    if (returnCode != LDAP_SUCCESS)
        goto FatalExit;

    returnCode = ldap_search_s(pLdapConnection, "DC=test, DC=com", LDAP_SCOPE_SUBTREE, "CN=ashwin", NULL, 0, &vLdapMessage);

    if (returnCode != LDAP_SUCCESS)
        goto FatalExit;

NormalExit:
    if (pLdapConnection != NULL)
        ldap_unbind_s(pLdapConnection);
    return 0;

FatalExit:
    if( pLdapConnection != NULL )
        ldap_unbind_s(pLdapConnection);
    printf( "\n\nERROR: 0x%x\n", returnCode);
    return returnCode;
}

検索は失敗します。ldap_search_s常に 1 を返します。Apache ディレクトリ サービスでの同じセットアップ テストは正常に機能します。

これが Windows AD で機能しない理由を誰かが指摘できますか? プログラムで何が間違っていますか?

4

1 に答える 1

2

Active Directory のフィルタリング構文は非常に冗長になる場合があります。私が知る限り、フィルターを少し変更する必要があります。これを試して :

(&(objectClass=user)(distinguishedName=CN=ashwin,CN=Users,DC=test,DC=com))

ただし、シングル ユーザー フィルタリングの場合は、sAMAccountName を使用してみます。これは一般に {FirstInitial}{LastName} 形式に従い、ユーザーに固有です (例: JSmith) :

(&(objectClass=user)(sAMAccountName=JSmith))

于 2013-06-21T15:29:27.230 に答える