2013-06-18 160 views
1

我在Windows 2008服務器上設置了Active Directory服務。 我已經添加了一個用戶,這裏是DN(DistingushedName)CN=ashwin,CN=Users,DC=test,DC=comLDAP的ldap_search_s()在Windows Active Directory上失敗

沒有爲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?該計劃有什麼錯誤?

回答

2

Active Directory過濾語法可能非常冗長。從我可以告訴,你只需要稍微修改你的過濾器。試試這個:

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

然而,對於單用戶過濾,我會嘗試使用sAMAccountName賦。這通常遵循{FirstInitial} {LastName}格式,並且對用戶來說是唯一的(例如JSmith):

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