The information in this article applies to:
SUMMARYCookies are a means by which a server application can cause a client to return information to the server with each HTTP request. This can be used to maintain a state with the client across multiple requests. Cookies are sent as part of the HTTP header in a client request or server response, and an ISAPI extension or filter DLL can readily send and retrieve them. This article is not meant to be a complete reference for cookies; it explains the basics of implementing them with the Microsoft Internet Server Application Programming Interface (ISAPI). For more information on cookies, please see the References section of this article. MORE INFORMATIONNOTE: Most of the code below is for an ISAPI DLL that does not use the MFC ISAPI classes or ISAPI Extension Wizard. For an ISAPI DLL that uses MFC, the functions called will be the MFC-wrapped versions. The syntax must be modified accordingly. Sending CookiesA cookie is sent to the client by the server in an HTTP "Set-Cookie:" header. This header can be added in an ISAPI filter with the AddResponseHeaders member function in the HTTP_FILTER_CONTEXT structure passed to the filter notification:
In the above example, "Cookie1" is the name of the cookie and "Value1" is
the value of the cookie. The "path=/" attribute tells the client to return
the cookie with all requests to that server. If unspecified, the client
assumes the path to be the same as that of the requested resource.
A cookie can also be added as an additional header in a call to ServerSupportFunction from within an ISAPI extension:
In an MFC ISAPI extension, headers should not be sent in this way; instead,
add the cookie to the output stream with the AddHeader function:
Note that the content type does not need to be "text/html"; cookies will
work for any content type.
Retrieving CookiesA cookie is returned to the server by the client in an HTTP "Cookie:" header. Multiple cookies can appear in this header, separated by semicolons. This header can be retrieved in an ISAPI filter responding to the SF_NOTIFY_PREPROC_HEADERS notification using the GetHeader member function in the HTTP_FILTER_PREPROC_HEADERS structure:
Or, a cookie can be retrieved in either a filter or extension using the
GetServerVariable member function in the HTTP_FILTER_CONTEXT and
EXTENSION_CONTROL_BLOCK structures:
In a filter:
Or, in an extension:
Cookie PersistenceThe cookies in the above examples will only be maintained by the client until the user exits the browser. The server can cause a cookie to be maintained by a browser for a longer period by specifying an "expires" attribute. This will cause the browser to store the cookie and continue returning it to the server with each request, until the cookie is expired:
Additional Notes
REFERENCES
For more information, please see the following sites:
http://home.netscape.com/newsref/std/cookie_spec.htmlRFC 2109 - HTTP State Management Mechanism: http://www.cis.ohio-state.edu/htbin/rfc/rfc2109.html Additional query words:
Keywords : iisapi |
Last Reviewed: August 26, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |