Newsgroups : Microsoft : microsoft.public.inetsdk.programming.wininet : 2005 Jun : WININET NTLM and default credentials for current user

www.cryer.info
Managed Newsgroup Archive

WININET NTLM and default credentials for current user

Subject:WININET NTLM and default credentials for current user
Posted by:"Matthias Schmidt" (matthias schmi..@discussions.microsoft.com)
Date:Wed, 15 Jun 2005 05:53:01

Hi all,

i have a problem with WININET and loggin in to a .NET webservice. All works
ok but the first time the call is made the user has to logon (the windows
login dialog pops up). My Question is: How can i configure Wininet with the
default credentials of the current logged in user? In .NET is use the line
webService.Credentials = CredentialCache.DefaultCredentials; and all works
fine.

Here is the pseudo code of my c++ Client:

HINTERNET hInternet   = NULL;
HINTERNET hConnection = NULL;
HINTERNET hRequest    = NULL;

hInternet = ::InternetOpen("SOAPCLIENT",
                           strProxy.GetLength() ?
(INTERNET_OPEN_TYPE_PRECONFIG | INTERNET_OPEN_TYPE_PROXY) :
INTERNET_OPEN_TYPE_DIRECT,
                           strProxy.GetLength() ? (LPCTSTR) strProxy : NULL,
                           NULL, 0);

hConnection = ::InternetConnect(hInternet,
                                url.GetHostName(),
                                (INTERNET_PORT)url.GetPortNumber(),
                                L"", L"",
                                INTERNET_SERVICE_HTTP,
                                INTERNET_FLAG_NO_UI,
                                NULL);

DWORD dwHttpsFlags = (url.GetScheme() == ATL_URL_SCHEME_HTTPS) ?
INTERNET_FLAG_SECURE : 0;

hRequest = ::HttpOpenRequest(hConnection, L"POST",
                             strUrl, HTTP_VERSION, NULL,
                             s_szAcceptTypes,
                             INTERNET_FLAG_NO_UI |
INTERNET_FLAG_KEEP_CONNECTION | dwHttpsFlags, NULL);

DWORD dwError       = 0;
DWORD dwStatusCode  = 0;
DWORD dwStatusSize  = sizeof(dwStatusCode);
bool  flAgain       = true;

// solange senden und mit vom Server benötigten Informationen anreichern, bis
// der Request durchläuft

while(flAgain)
{
    BOOL flSend = ::HttpSendRequest(hRequest, strHeaders,
(DWORD)strHeaders.GetLength(),        
                                    (void *)(LPCSTR)writeStream.m_str,
writeStream.m_str.GetLength());
    ::HttpQueryInfo(hRequest,
                    HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE,
                    &dwStatusCode, &dwStatusSize, NULL);

    switch(dwStatusCode)
    {
        case HTTP_STATUS_OK:
            flAgain = false;
            break;

        case HTTP_STATUS_DENIED:
        case HTTP_STATUS_PROXY_AUTH_REQ:
        {
            HWND hwndTop = ::GetForegroundWindow();
            dwError = ::InternetErrorDlg(hwndTop, hRequest,
ERROR_INTERNET_INCORRECT_PASSWORD,
                                         FLAGS_ERROR_UI_FILTER_FOR_ERRORS    |
                                         FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS |
                                         FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
                                         NULL);
            flAgain = (dwError == ERROR_INTERNET_FORCE_RETRY);
            break;
        }

        default:
            ...
    }
}


Thanks a lot
Matthias Schmidt

--
Matthias Schmidt        mailto:matthias.schmidt@sdm.de
sd&m AG         http://www.sdm.de
software design & management
Berliner Str. 76, 63065 Offenbach am Main, Germany
Phone +49 69 82901-251, Fax -200

Replies:

www.cryer.info
Managed Newsgroup Archive