简体   繁体   English

C ++ LDAP查询以找到memberOf

[英]C++ LDAP Query to locate memberOf

I am currently trying to perform an LDAP query in c++ to get the memberOf attribute of a given user. 我目前正在尝试在c ++中执行LDAP查询以获取给定用户的memberOf属性。 I have a function written that successfully gets the attribute if they are only in one group. 我编写了一个函数,如果它们仅在一组中,则可以成功获取属性。 The problem is that when they are in more than one group it only returns the first one. 问题在于,当它们在多个组中时,只会返回第一个。 When I look at the user in Active Directory browser I can see that it says they have 2 entries for memberOf, but when I use my function I only get the first. 当我在Active Directory浏览器中查看用户时,我可以看到它说他们有2个memberOf条目,但是当我使用函数时,我只会得到第一个。 Is there a way to modify my function to pull back all the entries or am I completely on the wrong path? 有没有办法修改我的功能以拉回所有条目,还是我完全走错了路?

bool FindADMembership(CStringArray* pUserArray,IDirectorySearch *pContainerToSearch, CString sAMAccountName){

if(pContainerToSearch==NULL||pUserArray==NULL)
    return false;

CString strSearchFilter;
strSearchFilter.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName=%s))", sAMAccountName);
BSTR b = strSearchFilter.AllocSysString();
LPOLESTR pszSearchFilter = b;
ADS_SEARCHPREF_INFO SearchPrefs;
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
DWORD dwNumPrefs = 1;

LPOLESTR pszColumn = NULL;    
ADS_SEARCH_COLUMN col;
HRESULT hr;

IADs    *pObj = NULL;
IADs    * pIADs = NULL;

ADS_SEARCH_HANDLE hSearch = NULL; 
hr = pContainerToSearch->SetSearchPreference( &SearchPrefs, dwNumPrefs);
if (FAILED(hr))
    return false;


LPOLESTR pszNonVerboseList[] = {L"memberOf"};
LPOLESTR szName = new OLECHAR[MAX_PATH];
int iCount = 0;


hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,    pszNonVerboseList,sizeof(pszNonVerboseList)/sizeof(LPOLESTR),&hSearch);
if ( SUCCEEDED(hr) )
{    
    hr = pContainerToSearch->GetFirstRow( hSearch);
    if (SUCCEEDED(hr))
    {
        while( hr != S_ADS_NOMORE_ROWS )
        {
            iCount++;
            while( pContainerToSearch->GetNextColumnName( hSearch, &pszColumn ) != S_ADS_NOMORE_COLUMNS )
            {
                hr = pContainerToSearch->GetColumn( hSearch, pszColumn, &col );
                if ( SUCCEEDED(hr) )
                {
                    if (0==wcscmp(L"memberOf", pszColumn))
                    {
                        wcscpy(szName,col.pADsValues->CaseIgnoreString);
                        pUserArray->Add(szName);
                    }

                    pContainerToSearch->FreeColumn( &col );
                }
                FreeADsMem( pszColumn );
            }  


            hr = pContainerToSearch->GetNextRow( hSearch);
        } 

    }
    else
        return false;

    pContainerToSearch->CloseSearchHandle(hSearch);
} 
else
    return false;

return true; }

Edit: 编辑:

After working on this here is the code to iterate through the results 完成此操作后,这里是遍历结果的代码

if (0==wcscmp(L"memberOf", pszColumn)) {
    for(int i = 0; i < col.dwNumValues; i++)
    {
        wcscpy(szName, col.pADsValues[i].CaseIgnoreString);
        pUserArray->Add(szName);
    }
}

You need to check the number of values and loop over them to get each in turn - pADSValues is an array that could contain > 1 string. 您需要检查值的数量并遍历它们以依次获取每个值pADSValues是一个数组,可以包含> 1个字符串。 The count is returned in dwNumValues . 该计数在dwNumValues返回。

if (0==wcscmp(L"memberOf", pszColumn))
{
    // This code should actually be a loop for i = 0 to dwNumValues - 1
    // with each loop checking the next array entry in the list pADsValues
    wcscpy(szName,col.pADsValues->CaseIgnoreString);
    pUserArray->Add(szName);
}

See docs here . 在这里查看文档。

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

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