HOWTO: Select Client Certificate in WinInetID: Q224282
|
This article explains how to select a client certificate using the WinInet APIs.
When accessing any SSL protected resource on a Web server that requires a valid client certificate, the WinInet HttpSendRequest API or MFC CInternetFile::SendRequest will fail initially with the following error:
To correctly handle this error, you can call InternetErrorDlg to bring up the client certificate dialog box (similar to the one in Internet Explorer) for the user to select the certificate. The code sample is shown as follows:ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED (12044)
...
while ( !HttpSendRequest( hReq, NULL, 0, NULL, 0 ) )
{
dwError = GetLastError();
if ( dwError == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED )
{
// Return ERROR_SUCCESS regardless of clicking on OK or Cancel
if( InternetErrorDlg( GetDesktopWindow(),
hReq,
ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
NULL) != ERROR_SUCCESS )
{
return ;
}
}
}
...
The same idea applies to MFC WinInet. In the case of MFC WinInet classes, the MFC methods corresponding to the WinInet APIs above are as follows:
if ( !HttpQueryInfo (hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwSize, NULL))
{
// Handle Error Here
}
Receiving 403 on the second retry of HttpSendRequest after InternetErrorDlg was called can indicate that user clicked Cancel.
For information on how to handle invalid server certificate authority error with WinInet, please see the following article in the Microsoft Knowledge Base:
Q182888 Handle Invalid Certificate Authority Error with WinInet
Additional query words:
Keywords : kbIE400 kbIE401 kbIE401sp1 kbIE401sp2 kbIE500
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 20, 1999