【问题标题】:CoCreateInstanceEx returns S_OK with invalid credentials on Win2003CoCreateInstanceEx 在 Win2003 上返回带有无效凭据的 S_OK
【发布时间】:2012-05-22 07:09:12
【问题描述】:

我正在编写一个远程连接到服务器的客户端应用程序。我需要模拟我的客户将与之合作的用户。

这是我的代码的摘录:

COAUTHIDENTITY coAuthIdentity, *pCoAuthIdentity;
COAUTHINFO coAuthInfo;

CComBSTR bstrDomain(domain), bstrServer(server);
CComBSTR bstrUser(user), bstrPassword(password);
CComBSTR bstrServerPrincName(domain);
bstrServerPrincName.Append(L"\\");
bstrServerPrincName.Append(server);

coAuthIdentity.Flags    = SEC_WINNT_AUTH_IDENTITY_UNICODE;
coAuthIdentity.Domain   = (USHORT *)bstrDomain.m_str;
coAuthIdentity.DomainLength = bstrDomain.Length();
coAuthIdentity.Password = (USHORT *)bstrPassword.m_str;
coAuthIdentity.PasswordLength   = bstrPassword.Length();
coAuthIdentity.User     = (USHORT *)bstrUser.m_str;
coAuthIdentity.UserLength   = bstrUser.Length();
pCoAuthIdentity         = &coAuthIdentity;
*ppCoAuthIdentity       = pCoAuthIdentity;

coAuthInfo.dwAuthnSvc       = RPC_C_AUTHN_DEFAULT;  // Have COM negotiate the best authentication service
coAuthInfo.dwAuthzSvc       = RPC_C_AUTHZ_NONE;
coAuthInfo.pwszServerPrincName  = bstrServerPrincName;

coAuthInfo.dwAuthnLevel     = RPC_C_AUTHN_LEVEL_DEFAULT;
coAuthInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
coAuthInfo.pAuthIdentityData    = &coAuthIdentity;
coAuthInfo.dwCapabilities       = EOAC_NONE;

COSERVERINFO coServerInfo;
::memset(&coServerInfo, 0, sizeof(COSERVERINFO));

coServerInfo.pwszName    = bstrServer;
coServerInfo.dwReserved1 = 0;
coServerInfo.pAuthInfo   = &coAuthInfo;
coServerInfo.dwReserved2 = 0;

MULTI_QI multiQI = { &__uuidof(IServer), 0, 0 };

TCHAR name [MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(name);
DWORD dwServer = CLSCTX_SERVER;
if (::GetComputerName(name, &size) != 0)
{
    if (_wcsicmp(name, coServerInfo.pwszName))
            dwServer = CLSCTX_REMOTE_SERVER;
    else
        dwServer = CLSCTX_LOCAL_SERVER;
}
else
{
    DWORD dwError = GetLastError();
    return HRESULT_FROM_WIN32(dwError);
}    

hr = CoCreateInstanceEx(
    __uuidof(IServer),
        NULL,
    dwServer,
    &coServerInfo,
    1,
    &multiQI);

当我将客户端和服务器安装在同一台计算机上时,会出现我的问题。在 Windows 2003 中,当我将任何用户(有效或无效)的凭据传递到我的服务器时,CoCreateInstanceEx 函数总是返回 S_OK。

问题是在服务器端使用的用户不是模拟的用户,而是启动我的客户端进程的用户。

使用 Windows 2008 可以正常工作。有什么想法吗?

提前致谢。

【问题讨论】:

    标签: c++ windows com


    【解决方案1】:

    这很有趣。 http://msdn.microsoft.com/en-us/library/aa913628.aspx

    也许使用注册表值而不是显式的身份验证值? CoInitializeSecurity 在其他地方被调用?我唯一能想到的。

    【讨论】:

      【解决方案2】:

      Don Box - Essential COM 书指出:“在撰写本文时,同一机器通信不支持 COAUTHIDENTITY”(第 289 页)。此外,Thuan Thai - Learning DCOM 书指出:“设置身份验证身份仅适用于客户端和服务器组件在不同机器上运行时”(第 9 章 - 客户端安全性)。

      因此,我怀疑您的同机问题是由长期存在的 Windows 限制引起的。我还能够在 Windows 10(版本 1803)上重现该问题。

      【讨论】:

        猜你喜欢
        • 2019-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        相关资源
        最近更新 更多