HOWTO: Read Cookies Using ASP and Visual C++

ID: Q241492


The information in this article applies to:
  • Active Server Pages


SUMMARY

Please note that the sample described in this article is dependent on another article from the Microsoft Knowledge Base, Q240191 - HOWTO: 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.



MORE INFORMATION

If you want to read cookies using ASP and Visual C++ use the following steps.

  1. Open the ATL project you created in Q240191.


  2. 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.


  3. From the Add Method dialog box, enter "ReadTestCookie" (minus the quotes) as the method name, and click OK.


  4. From the Class View tab, expand the CASPCookies class, then expand the IASPCookies interface that appears.


  5. Double-click on the "ReadTestCookies" method, the implementation of CASPCookies::ReadTestCookies should appear.


  6. 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); 


  7. From the Build menu, select "Build KBWriteCookies.dll" to build your project.


  8. 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()
    %>  


  9. View the ASP page in your Web browser and you should see the following displayed on the page:
    
       CookieValue  


  • 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:
    Q239445 HOWTO: Obtain ObjectContext with ObjectControl Inside VC COM DLL From ASP and MTS



REFERENCES

For additional information, please click the article number(s) below to view the article(s) in the Microsoft Knowledge Base:

Q240191 HOWTO: Set Cookies Using ASP and Visual C++
Q239445 HOWTO: Obtain ObjectContext with ObjectControl Inside VC COM DLL from ASP and MTS
Q166279 HOWTO: Lifetime of a COM Component Under IIS, ASP, and RDS
Q159402 HOWTO: How To Use Response.Redirect in a Server Script

Additional query words:

Keywords : kbASP kbASP400 kbASPObj kbCOMt kbVC kbVC600 kbGrpASP kbDSupport kbCookies
Version : winnt:
Platform : winnt
Issue type : kbhowto


Last Reviewed: October 12, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.