C++/Qt : How to invoke OS Windows Security dialog for proxy credentials?

1.3k views Asked by At

My application needs to connect to internet through proxy. I am making use of QNetworkProxyFactory::setUseSystemConfiguration to accomplish it. For Authenticated proxies, i need to pass user credentials to the proxy object. I don't want to build a custom dialog to get these details instead i want to make use of OS dialog for it which was shown by internet explorer(Please refer to the screenshot). I have tried using CredUIPromptForCredentials to show credentials dialog but this UI is not same as windows security dialog.

Can someone throw some light on which MSDN API to be used to show the below dialog from C++ ?

Dialog shown by Internet explorer for proxy credentials

Below is the one which was shown from CredUIPromptForCredentials

enter image description here

1

There are 1 answers

0
Engineer On

I am able to show WindowsSecurity dialog to ask for Proxy credentials. Here is the code. Will have to fine tune it bit but it is working.

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;
            }
        }

}