Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 241492 - Last Review: July 2, 2004 - Revision: 1.1
How To Read Cookies Using ASP and Visual C++
This article was previously published under Q241492
Please note that the sample described in this article is dependent on another article from the Microsoft Knowledge Base, Q240191 - How To Set Cookies Using ASP and Visual C++. Please follow the steps outlined in this KB article before proceeding with this article as many of the basic steps are assumed to have been completed.
This article describes how to read cookies from the Active Server Pages (ASP) Request object using a component written using the Active Template Library (ATL) and Visual C++.
Unlike reading cookies by means of a script on an ASP page, or from a Visual Basic component, you must use three (3) interfaces implemented by ASP to use this functionality. These interfaces (in order of use) are IResponse, IRequestDictionary, and IReadCookie.
The functional flow of reading cookies from a Visual C++ component is as follows:
- Obtain the Response object from IScriptingContext or IObjectContext.
- Use the Response object to obtain an IRequestDictionary containing the Cookies collection.
- Use the Cookies collection to obtain the IReadCookie object, which will allow you to read cookies sent by the browser to the server.
If you want to read cookies using ASP and Visual C++ use the following steps.
- Open the ATL project you created in Q240191.
- From the Class View tab, right-click the IASPCookies interface and select the Add Method from the Context menu. The Add Method dialog box should appear.
- From the Add Method dialog box, enter "ReadTestCookie" (minus the quotes) as the method name, and click OK.
- From the Class View tab, expand the CASPCookies class, then expand the IASPCookies interface that appears.
- Double-click on the "ReadTestCookies" method, the implementation of CASPCookies::ReadTestCookies should appear.
- Paste the following code into the CASPCookies::ReadTestCookies() method implementation.
HRESULT hr = NOERROR;
IRequestDictionary* pDict = NULL; // First step to get cookie collection
IReadCookie* pReadCookie = NULL; // Cookie collection
VARIANT vtCookieDict; // ptr to dispinterface IReadCookie
VARIANT vtCookieName; // For the name of the cookie
VARIANT vtCookieKey; // For the cookie key<BR/>
VARIANT vtCookieVal; // For the cookie value
BSTR bstrCookieName; // For the cookie name
BSTR bstrCookieKey; // For the key
// Initialize the temp vars
bstrCookieName = SysAllocString(L"KBTESTCOOKIES");
bstrCookieKey = SysAllocString(L"CookieKey");
// Set the name of the cookie and change the type
// If you don't IRequestDictionary::get_Item() will
// fail because the variant will come through as VT_EMPTY
vtCookieName.bstrVal = bstrCookieName;
vtCookieName.vt = VT_BSTR;
// Set the key we'll use for the cookie's key/value pair.
// Make sure to change the type or put_Item() will fail.
vtCookieKey.bstrVal = bstrCookieKey;
vtCookieKey.vt = VT_BSTR;
// First you have to get the IRequestDictionary
// then call get_Item() to get the actual IReadCookie
// object.
hr = m_piRequest->get_Cookies(&pDict);
if(SUCCEEDED(hr))
{
// Request access to the cookie named "KBTESTCOOKIES" specifically.
// vtCookieDict will return with the ptr
// to the IReadCookie object
hr = pDict->get_Item(vtCookieName, &vtCookieDict);
if(SUCCEEDED(hr))
{
// Got the pointer to IReadCookie now make it friendly.
pReadCookie = (IReadCookie*)(vtCookieDict.pdispVal);
hr = pReadCookie->get_Item(vtCookieKey, &vtCookieVal);
// release what we're done with
pReadCookie->Release();
pDict->Release();
if(SUCCEEDED(hr))
{
// Write the cookie value out to the page
hr = m_piResponse->Write(vtCookieVal);
}
}
else
{
pDict->Release();
}
}
SysFreeString(bstrCookieName);
SysFreeString(bstrCookieKey);
SysFreeString(bstrCookieVal);
return(hr);
- From the Build menu, select "Build KBWriteCookies.dll" to build your project.
-
Create a new ASP page and paste in the following code and save the file:
<%
Dim obj
Set obj = Server.CreateObject("KBWriteCookies.ASPCookies")
Call obj.ReadTestCookie()
%>
-
View the ASP page in your Web browser and you should see the following displayed on the page:
- This article uses the IScriptingContext interface to gain access to the ASP intrinsic objects, however it is strongly recommended that you use Microsoft Transaction Server's ObjectContext to obtain the ASP intrinsic objects, as support for IScriptingContext may be removed from future versions of ASP.
For additional information, please click the article number below
to view the article in the Microsoft Knowledge Base:
239445Â
(http://kbalertz.com/Feedback.aspx?kbNumber=239445/EN-US/
)
How To Obtain ObjectContext with ObjectControl Inside VC COM DLL From ASP and MTS
For additional information, please click the article number(s) below
to view the article(s) in the Microsoft Knowledge Base:
240191Â
(http://kbalertz.com/Feedback.aspx?kbNumber=240191/EN-US/
)
How To Set Cookies Using ASP and Visual C++
239445Â
(http://kbalertz.com/Feedback.aspx?kbNumber=239445/EN-US/
)
How To Obtain ObjectContext with ObjectControl Inside VC COM DLL from ASP and MTS
166279Â
(http://kbalertz.com/Feedback.aspx?kbNumber=166279/EN-US/
)
How To Lifetime of a COM Component Under IIS, ASP, and RDS
159402Â
(http://kbalertz.com/Feedback.aspx?kbNumber=159402/EN-US/
)
How To How To Use Response.Redirect in a Server Script
APPLIES TO
- Microsoft Active Server Pages 4.0
| kbaspobj kbcodesnippet kbcookie kbhowto KB241492 |
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate