我尝试使用 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;
}