简体   繁体   English

LDAP获取完整的用户名C ++

[英]LDAP Getting Full user Name C++

I'm writing a little Application at my Office. 我在办公室写一个小应用程序。 In this Application i have to get the full UserName. 在此应用程序中,我必须获取完整的用户名。 It's written in Qt / C++ 它是用Qt / C ++编写的

I like to get this Information over LDAP. 我喜欢通过LDAP获取此信息。 Now i can connect to our Active Directory Server and can connect to the Search function. 现在,我可以连接到我们的Active Directory服务器,并且可以连接到搜索功能。

This information is from an example written by Microsoft. 此信息来自Microsoft编写的示例。

But at this Point the Example confuses me. 但是在这一点上,示例使我感到困惑。 I Don't get it how i can give this Server an Username and receive the Full Name. 我不知道如何为该服务器提供用户名并接收全名。

Link to the Microsoft example: http://msdn.microsoft.com/en-us/library/windows/desktop/aa367016(v=vs.85).aspx 链接到Microsoft示例: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/aa367016(v=vs.85).aspx

Can someone give me an Advice how i can get this Information? 有人可以给我建议我如何获取此信息?

Thanks and With best regards Chris ;) 谢谢,并最好的问候克里斯;)

PS Sorry for the bad English. PS对不起,英语不好。 PSS and if i missed Something or you need more Information just say it. PSS,如果我错过了某些内容,或者您​​需要更多信息,请说出来。

LDAP_query.cpp: 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);
}

It appears that you are requesting the cn or commonName , sometimes the value of that attribute is the full name. 似乎您正在请求cncommonName ,有时该属性的值是全名。 If it is not, try adding givenName and sn to the list of requested attributes. 如果不是,请尝试将givenNamesn添加到请求的属性列表中。 givenName is the first name sn is the surname or last name. givenName是名字snsurname或名字。

Just a little other Question. 只是其他一些问题。 Below you see the Lines which get the things i Need. 在下面,您会看到获得我需要的东西的线。 It sounds a little Stupid but when i run the code the Attributes which are in pMyAttributes[0], will never be Displayed. 这听起来有点愚蠢,但是当我运行代码时,pMyAttributes [0]中的Attributes将永远不会显示。 I don't care because i don't need this Information but it don's seems like it is Right. 我不在乎,因为我不需要此信息,但似乎它是正确的。 I'm a little Scared that i will get later wrong Information and my Programm will get Error's. 我有点害怕我以后会得到错误的信息,而我的程序也会得到错误的信息。

Settings Filters With "PWCHAR pMyAttributes[4];" 使用“ PWCHAR pMyAttributes [4];”的设置过滤器 will Display nothing 什么都不显示

But Setting Filter "PWCHAR pMyAttributes[5];" 但是将过滤器设置为“ PWCHAR pMyAttributes [5];” like that and i get Everything i wanted. 这样,我得到了我想要的一切。 Can someone Explain this ? 有人可以解释一下吗?

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;

Code for Displaying the user's: 显示用户的代码:

// 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM