2
我已經問過如何獲取用戶名。 How can I get the user name who invoked the method of COM server? 現在我還需要獲取SID。我該怎麼做呢?如何獲取調用COM服務器方法的用戶的SID?
OpenProcessToken方法不起作用,因爲此函數失敗,並顯示ERROR_BAD_IMPERSONATION_LEVEL錯誤。
我已經問過如何獲取用戶名。 How can I get the user name who invoked the method of COM server? 現在我還需要獲取SID。我該怎麼做呢?如何獲取調用COM服務器方法的用戶的SID?
OpenProcessToken方法不起作用,因爲此函數失敗,並顯示ERROR_BAD_IMPERSONATION_LEVEL錯誤。
UPDATE 爲了做到這一點,客戶端應該允許服務器通過配置與CoSetProxyBlanket API代理,以獲取其信息,如下圖所示:
HRESULT hr;
if(FAILED(hr = ::CoSetProxyBlanket(
unk // An interface the client uses to access the com server
, RPC_C_AUTHN_DEFAULT
, RPC_C_AUTHZ_DEFAULT
, NULL
, RPC_C_AUTHN_LEVEL_DEFAULT
, RPC_C_IMP_LEVEL_DELEGATE // This flag should be *_DELEGATE or *_IMPERSONATE or *_IDENTIFY
, NULL
, EOAC_DYNAMIC_CLOAKING)))
throw com_exception(hr, "Failed to set proxy blanket");
然後被假冒(CoImpersonateClient)你可能用OpenThreadToken訪問用戶的令牌。
UPDATE
結束處,之後,您可以使用GetTokenInformation API如下:
DWORD tokenSize = 0;
::GetTokenInformation(token, TokenUser, NULL, 0, &tokenSize);
....
TOKEN_USER *tokenInfo; // should point to a memory location of size tokenSize.
....
if(::GetTokenInformation(token, infoClass, tokenInfo, tokenSize, &tokenSize) == FALSE)
throw win32_exception(::GetLastError(), "Failed to obtain token information");
tokenInfo將包含類型PSID領域User.Sid。