2012-08-15 2 views
0

내 사무실에서 약간의 신청서를 작성하고 있습니다. 이 응용 프로그램에서는 전체 UserName을 가져와야합니다. Qt/C++로 작성되었습니다LDAP 전체 사용자 이름 얻기 C++

나는 LDAP를 통해이 정보를 얻고 싶습니다. 이제 Active Directory 서버에 연결할 수 있으며 검색 기능에 연결할 수 있습니다.

이 정보는 Microsoft에서 작성한 사례에서 발췌 한 것입니다.

그러나이 지점에서는 예제가 나를 혼란스럽게합니다. I이 서버에 사용자 이름을 부여하고 전체 이름을받는 방법을 모르겠다. 마이크로 소프트 예

링크 : http://msdn.microsoft.com/en-us/library/windows/desktop/aa367016(v=vs.85).aspx

누군가가 나에게 내가이 정보를 얻을 수있는 방법 조언을 줄 수 있습니까?)

P.S.하고, 안부 크리스와

감사합니다 나쁜 영어를 유감스럽게 생각합니다. P.S.S 그리고 내가 뭔가를 놓친 경우 또는 더 많은 정보가 필요하다면 그냥 말해야합니다.

LDAP_query.cpp :

#include <settings.h> 
#include <ui_settings.h> 
#include <mainwindow.h> 
#include <ui_mainwindow.h> 
#include <QtGui> 
#include <QString> 
//Header Files for LDAP Query 
#include <windows.h> 
#include <winldap.h> 
#include <winber.h> 
#include <rpc.h> 
#include <rpcdce.h> 
#include <stdio.h> 

void MainWindow::LDAP_query(QString name) 
{ 

//Name contains the username 

PWCHAR hostName = NULL; 
PWCHAR Domain = NULL; 
PWCHAR pMyDN = NULL; 
PWCHAR pUserName; 
PWCHAR pPassword; 
LDAP* pLdapConnection = NULL; 
ULONG version = LDAP_VERSION3; 
ULONG getOptSuccess = 0; 
ULONG connectSuccess = 0; 
INT returnCode = 0; 

// Convert String hostname to a wchar_t* 
char *hostName_2 = "Server.office.com"; 
QString Test = QString::fromAscii(hostName_2); 
hostName = (WCHAR*)(Test.utf16()); 

//Connverting Char to WCHAR to connect to Directory 
char *pMyDN_2 = "Ou=directory,Dc=Name,DC=office"; 
QString test2 = QString::fromAscii(pMyDN_2); 
pMyDN = (WCHAR*)(test2.utf16()); 

//Open Connection 
pLdapConnection = ldap_init(hostName, LDAP_PORT); 
//Setting Connection Parm's 

    ldap_set_option(pLdapConnection, LDAP_OPT_PROTOCOL_VERSION, (void*)&version); 
ldap_connect(pLdapConnection, NULL); 
returnCode = ldap_bind_s(pLdapConnection, pMyDN, NULL, LDAP_AUTH_NEGOTIATE); 

if(returnCode == LDAP_SUCCESS) 
{ 
    ui->InputA->setText("Connection sucessfull"); 
} 
else 
{ 
    ui->InputA->setText("Connection unsucessfull"); 
} 

//Variables for Search Results  
LDAPMessage* pSearchResult; 
PWCHAR pMyFilter = NULL; 
char *pMyFilter_2 = "(&(objectCategory=person)(objectClass=user))"; 
QString Test7 = QString::fromAscii(pMyFilter_2); 
pMyFilter = (WCHAR*)(Test7.utf16()); 

PWCHAR pMyAttributes[6]; 
ULONG errorCode = LDAP_SUCCESS; 

pMyAttributes[0] = (WCHAR*)QString("cn").utf16(); 
pMyAttributes[1] = (WCHAR*)QString("company").utf16(); 
pMyAttributes[2] = (WCHAR*)QString("department").utf16(); 
pMyAttributes[3] = (WCHAR*)QString("telephoneNumber").utf16(); 
pMyAttributes[4] = (WCHAR*)QString("memberOf").utf16(); 
pMyAttributes[5] = NULL; 

errorCode = ldap_search_s(
        pLdapConnection, // Session handle 
        pMyDN,    // DN to start search 
        LDAP_SCOPE_SUBTREE, // Scope 
        pMyFilter,   // Filter 
        pMyAttributes,  // Retrieve list of attributes 
        0,     // Get both attributes and values 
        &pSearchResult); // [out] Search results 

if (errorCode != LDAP_SUCCESS) 
    { 
     ui->InputB->setText("ldap_search_s failed with"); 
     ldap_unbind_s(pLdapConnection); 
     if(pSearchResult != NULL) 
      ldap_msgfree(pSearchResult); 
    } 
    else 
     ui->InputB->setText("ldap_search succeeded \n"); 


//here i like to receive the user's full name 

//Closing Connection 
ldap_unbind(pLdapConnection); 

ui->Test_Ausgabe -> setText(name); 
} 

답변

0

당신이 cn 또는 commonName, 때로는 그 속성의 값이 전체 이름입니다을 요청하는 것으로 보인다. 그렇지 않은 경우 요청 된 속성 목록에 givenNamesn을 추가하십시오. givenNamesn의 첫 번째 이름은 surname 또는 성입니다.

+0

내가. 나는 useres의 전체 이름을 얻었다. "cn"을 사용하면 qDebug가 사용자의 전체 이름을 보여주었습니다. 이제 Windows에서 사용자 이름을 필터링하면됩니다 (그리고 AD에서 가져 오는 것). 그런 다음 큰 걸음. @Terry Gardner – Chris9327

0

조금 다른 질문. 아래에 내가 필요한 것을 얻는 라인이 있습니다. 약간 어리석은 소리지만 코드를 실행하면 pMyAttributes [0]에있는 특성이 절대로 표시되지 않습니다. 나는이 정보가 필요 없기 때문에 걱정하지 않는다. 그러나 그것이 옳은 것처럼 보인다. 나는 조금 나중에 무서워서 정보를 얻을 것이고 나의 Programm은 Error를 얻을 것이다.

설정 필터 "PWCHAR pMyAttributes [4];" 아무것도 표시하지 않습니다.

하지만 "PWCHAR pMyAttributes [5];" 그런 것과 나는 내가 원하는 모든 것을 얻는다. 누군가 설명 할 수 있습니까? 사용자의 표시에 대한

PWCHAR pMyAttributes[4]; 
ULONG errorCode = LDAP_SUCCESS; 

pMyAttributes[0] = (WCHAR*)QString("sn").utf16(); 
pMyAttributes[1] = (WCHAR*)QString("mailNickname").utf16(); 
pMyAttributes[2] = (WCHAR*)QString("cn").utf16(); 
pMyAttributes[3] = NULL; 

코드 : 몇 가지 오류를 받고 이후 지금

// Get the number of entries returned. 
    ULONG numberOfEntries; 

numberOfEntries = ldap_count_entries(
        pLdapConnection, // Session handle 
        pSearchResult);  // Search result 

/*if(numberOfEntries == NULL) 
{ 
    qDebug("ldap_count_entries failed with 0x%0lx \n",errorCode); 
    ldap_unbind_s(pLdapConnection); 
    if(pSearchResult != NULL) 
     ldap_msgfree(pSearchResult); 
} 
else 
    qDebug("ldap_count_entries succeeded \n"); 
*/ 
qDebug("The number of entries is: %d \n", numberOfEntries); 


//---------------------------------------------------------- 
// Loop through the search entries, get, and output the 
// requested list of attributes and values. 
//---------------------------------------------------------- 
LDAPMessage* pEntry = NULL; 
PWCHAR pEntryDN = NULL; 
ULONG iCnt = 0; 
char* sMsg; 
BerElement* pBer = NULL; 
PWCHAR pAttribute = NULL; 
PWCHAR* ppValue = NULL; 
ULONG iValue = 0; 

for(iCnt=0; iCnt < numberOfEntries; iCnt++) 
{ 
    // Get the first/next entry. 
    if(!iCnt) 
     pEntry = ldap_first_entry(pLdapConnection, pSearchResult); 
    else 
     pEntry = ldap_next_entry(pLdapConnection, pEntry); 


    // Output the entry number. 
    qDebug("ENTRY NUMBER %i \n", iCnt); 

    // Get the first attribute name. 
    pAttribute = ldap_first_attribute(
        pLdapConnection, // Session handle 
        pEntry,   // Current entry 
        &pBer);   // [out] Current BerElement 

    // Output the attribute names for the current object 
    // and output values. 
    while(pAttribute != NULL) 
    { 
     // Output the attribute name. 
     QString abc = QString::fromWCharArray(pAttribute); 
     qDebug() << "abc" << abc; 

     // Get the string values. 

     ppValue = ldap_get_values(
         pLdapConnection, // Session Handle 
         pEntry,   // Current entry 
         pAttribute);  // Current attribute 

     // Print status if no values are returned (NULL ptr) 
     if(ppValue == NULL) 
     { 
      qDebug(": [NO ATTRIBUTE VALUE RETURNED]"); 
     } 

     // Output the attribute values 
     else 
     { 
      iValue = ldap_count_values(ppValue); 
      if(!iValue) 
      { 
       qDebug(": [BAD VALUE LIST]"); 
      } 
      else 
      { 
       // Output the first attribute value 
       QString abc2 = QString::fromWCharArray(*ppValue); 
       qDebug()<< "abc2" << abc2; 
       qDebug()<< "iValue" << iValue; 

       // Output more values if available 
       ULONG z; 
       for(z=1; z<iValue; z++) 
       { 
        QString abc3 = QString::fromWCharArray(ppValue[z]); 
        qDebug() << "abc3" << abc3; 
       } 
      } 
     } 

     // Free memory. 
     if(ppValue != NULL) 
      ldap_value_free(ppValue); 
     ppValue = NULL; 
     ldap_memfree(pAttribute); 

     // Get next attribute name. 
     pAttribute = ldap_next_attribute(
         pLdapConnection, // Session Handle 
         pEntry,   // Current entry 
         pBer);    // Current BerElement 
     qDebug("\n"); 
    } 

    if(pBer != NULL) 
     ber_free(pBer,0); 
    pBer = NULL; 
} 


//Closing Connection 
ldap_unbind(pLdapConnection); 

ui->Test_Ausgabe -> setText(name); 
} 
관련 문제