-1

我的应用程序需要通过代理连接到互联网。我正在使用 QNetworkProxyFactory::setUseSystemConfiguration 来完成它。对于经过身份验证的代理,我需要将用户凭据传递给代理对象。我不想构建一个自定义对话框来获取这些详细信息,而是我想使用 Internet Explorer 显示的操作系统对话框(请参阅屏幕截图)。我曾尝试使用CredUIPromptForCredentials显示凭据对话框,但此 UI 与 Windows 安全对话框不同。

有人可以阐明使用哪个 MSDN API 从 C++ 显示以下对话框吗?

Internet Explorer 显示的代理凭据对话框

下面是从CredUIPromptForCredentials显示的一个

在此处输入图像描述

4

1 回答 1

1

我可以显示 WindowsSecurity 对话框来询问代理凭据。这是代码。将不得不对其进行微调,但它正在工作。

void getCredentials(const char * proxyIp, int proxyPort, char * proxType, QString &user, QString &password)
{

    Log.info("Credentials", L"About to read credentials for [%hs] [%d] [%hs]",proxyIp,proxyPort,proxType);

        HRESULT hr = S_OK;
        DWORD   dwResult;
        PVOID   pvInAuthBlob = NULL;
        ULONG   cbInAuthBlob = 0;
        PVOID   pvAuthBlob = NULL;
        ULONG   cbAuthBlob = 0;
        CREDUI_INFOW ui;
        ULONG   ulAuthPackage = 0;
        BOOL    fSave = FALSE;
        TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH+1];
        TCHAR pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1];
        TCHAR domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH+1];
        DWORD maxLenName =  CREDUI_MAX_USERNAME_LENGTH+1;
        DWORD maxLenPassword =  CREDUI_MAX_PASSWORD_LENGTH+1;
        DWORD maxLenDomain = CREDUI_MAX_DOMAIN_TARGET_LENGTH+1;


        // Display a dialog box to request credentials.
        ui.cbSize = sizeof(ui);
        ui.hwndParent = GetConsoleWindow();
        ui.pszMessageText = L"The Proxy Server requires user name and password";
        ui.pszCaptionText = L"Proxy Authentication";
        ui.hbmBanner = NULL;

        dwResult = CredUIPromptForWindowsCredentialsW(
                       &ui,             // Customizing information
                       0,               // Error code to display
                       &ulAuthPackage,  // Authorization package
                       pvInAuthBlob,    // Credential byte array
                       cbInAuthBlob,    // Size of credential input buffer
                       &pvAuthBlob,     // Output credential byte array
                       &cbAuthBlob,     // Size of credential byte array
                       &fSave,          // Select the save check box.
                       CREDUIWIN_GENERIC
                       );
        if (dwResult == NO_ERROR)
        {
            Log.info("Credentials", L"cred read success");
            CredUnPackAuthenticationBufferW(0,
                                             pvAuthBlob,
                                            cbAuthBlob,
                                            pszName,
                                            &maxLenName,
                                            domain,
                                            &maxLenDomain,
                                            pszPwd,
                                            &maxLenPassword);
            SecureZeroMemory(pvAuthBlob, cbAuthBlob);
            CoTaskMemFree(pvAuthBlob);
            pvAuthBlob = NULL;

            Log.info("Credentials", L"u [%ls] p [%ls] d[%ls]",QString::fromWCharArray(pszName).toStdWString().c_str(),
                                                                QString::fromWCharArray(pszPwd).toStdWString().c_str(),
                                                                QString::fromWCharArray(domain).toStdWString().c_str());

            user = QString::fromWCharArray(pszName).toStdWString();
            password = QString::fromWCharArray(pszPwd).toStdWString();

            SecureZeroMemory(pszName, sizeof(pszName));
            SecureZeroMemory(pszPwd, sizeof(pszPwd));


        }else
        {
            Log.info("Credentials", L"cred read fail");

            hr = HRESULT_FROM_WIN32(dwResult);
            if (pvInAuthBlob)
            {
                SecureZeroMemory(pvInAuthBlob, cbInAuthBlob);
                CoTaskMemFree(pvInAuthBlob);
                pvInAuthBlob = NULL;
            }
        }

}
于 2013-09-25T08:56:30.167 回答