How To Read Client Certificate in ASP Component with Visual C++ (297111)
The information in this article applies to:
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Server 5.0
This article was previously published under Q297111 SUMMARY
This article describes how you can read Client Certificates from the Active Server Pages (ASP) Request object by using a component that is written with the Active Template Library (ATL) and with Microsoft Visual C++.
Unlike when you read Certificates by means of a script on an ASP page or from a Microsoft Visual Basic component, you must use two interfaces implemented by ASP to use this functionality. These interfaces are (in order of use):
- IResponse
- IRequestDictionary
The functional flow of Client Certificate from a Visual C++ component follows: - Obtain the Response object from the IScriptingContext or the IObjectContext.
- Use the Response object to obtain an IRequestDictionary interface that contains the Client Certificate collection.
- Get the "Certificate" item from the collection.
- Obtain a default member (value property) from the IDispatch interface returned in the earlier step.
MORE INFORMATION- Create a new Visual C++ 6.0 project by using the ATL COM AppWizard.
- From the list of available server types, make sure DLL is selected, and then click Finish.
- On the Insert menu, click New ATL Object.
- From the Objects category, click ActiveX Server Component.
After you have a Component Object Model (COM) object, add a method that obtains a Client Certificate, and then save the Client Certificate to the file. You can use code similar to the following code sample:
STDMETHODIMP CComClass::GetCert(BSTR* szCert)
{
TCHAR szTemp [256];
CComPtr<IRequestDictionary> piCert;
CComVariant vt(L"Certificate"), vtRet;
*szCert = NULL;
HRESULT hr = m_piRequest->get_ClientCertificate (&piCert);
if (FAILED(hr))
{
ATLTRACE (szTemp, L"get_ClientCertificate failed: 0x%x\n", hr);
return hr;
}
hr = piCert->get_Item (vt, &vtRet);
if (FAILED(hr))
{
ATLTRACE (szTemp, L"get_Item failed: 0x%x\n", hr);
return hr;
}
if(vtRet.vt == VT_DISPATCH)
{
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
CComVariant VarResult;
CComPtr <IDispatch> pDisp (vtRet.pdispVal);
hr = pDisp->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dispparamsNoArgs, &VarResult,
NULL, NULL);
if (FAILED(hr))
{
ATLTRACE (szTemp, L"Invoke failed: 0x%x\n", hr);
return hr;
}
DWORD dwSize = SysStringLen (VarResult.bstrVal);
CHAR *szCertANSI= new CHAR [dwSize];
if (!WideCharToMultiByte (CP_ACP, 0, VarResult.bstrVal, dwSize,
szCertANSI, dwSize, NULL, NULL))
{
DWORD dwError = GetLastError();
ATLTRACE (szTemp, L"Conversion failed: 0x%x\n",
dwError);
delete [] szCertANSI;
return HRESULT_FROM_WIN32 (dwError);
}
ofstream myFile ("c:\\temp\\Cert.cer", ios::binary );
myFile.write ((char*)szCertANSI, dwSize);
myFile.close();
delete [] szCertANSI;
*szCert = SysAllocString (L"File with Cert is saved!");
return S_OK;
}
return E_INVALIDARG;
}
NOTE: The m_piRequest is a pointer to ASP intrinsics interface IRequest, which is available to your component. See the platform software development kit (SDK) documentation for information about how you can obtain a pointer to IRequest in the component.
You can use the following ASP code to test the COM object:
<%
Dim myObj
Set myObj
= Server.CreateObject ("AspCom.ComClass")
Response.Write myObj.GetCert ()
%>
NOTE: You must make sure to use the correct ProgId property in the CreateObject function so that the CreateObject reflects the correct name of your component.
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
152071 How To Calling Hidden Default Method of an OLE Automation Collection
Modification Type: | Minor | Last Reviewed: | 7/2/2004 |
---|
Keywords: | kbhowto KB297111 |
---|
|