我尝试使用IDirectorySearch从Active Directory中读取一些属性值。 当我运行本地程序时,代码工作正常。我写了IIS7扩展和我的代码运行到w3wp.exe。当代码运行到w3wp.exe时,ExecuteSearch方法找到我需要的东西,但GetColumn只返回pszAttrName和dwADsType。在pADsValues中,我看到错误的值(或未初始化的)
代码:
std::string strFromHex(BYTE* data, int length)
{
std::stringstream ss;
ss << std::hex;
for( size_t i=0;i != length; ++i )
ss << (size_t)data[i];
return ss.str();
}
std::string GetGUID()
{
std::string guid;
IDirectorySearch* dSearch;
hr = ADsOpenObject(L"LDAP://DOMEN", L"UserLogin", L"UserPass", ADS_SECURE_AUTHENTICATION, IID_IDirectorySearch, (void**)&dSearch);
if(FAILED(hr))
return guid;
LPWSTR searchFilter = L"(&(objectClass=user)(| (userPrincipalName=userLogin*)))";
LPWSTR att[] = { L"objectGUID" };
DWORD attCount = 1;
ADS_SEARCH_HANDLE hSearch;
ADS_SEARCH_COLUMN col;
hr = dSearch->ExecuteSearch( searchFilter, att, attCount, &hSearch);
if(FAILED(hr))
return guid;
hr = dSearch->GetFirstRow(hSearch);
while(S_OK == hr)
{
for(DWORD i = 0; i < attCount; i++)
{
hr = dSearch->GetColumn(hSearch, att[i], &col);
if(SUCCEEDED(hr))
{
if( col.dwADsType == ADSTYPE_OCTET_STRING )
guid = strFromHex( col.pADsValues[0].OctetString.lpValue, col.pADsValues[0].OctetString.dwLength );
dSearch->FreeColumn(&col);
}
}
hr = dSearch->GetNextRow(hSearch);
}
hr = dSearch->CloseSearchHandle(hSearch);
return guid;
}