【发布时间】:2012-03-02 10:53:49
【问题描述】:
我已经询问了如何获取用户名。 How can I get the user name who invoked the method of COM server? 现在我也需要获取 SID。我该怎么做?
OpenProcessToken 方法不起作用,因为此函数失败并出现 ERROR_BAD_IMPERSONATION_LEVEL 错误。
【问题讨论】:
我已经询问了如何获取用户名。 How can I get the user name who invoked the method of COM server? 现在我也需要获取 SID。我该怎么做?
OpenProcessToken 方法不起作用,因为此函数失败并出现 ERROR_BAD_IMPERSONATION_LEVEL 错误。
【问题讨论】:
更新 为此,客户端应允许服务器通过使用 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 访问用户的令牌。
更新结束
之后,您可以使用 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 字段。
【讨论】: