HttpRequest.Cookies Property
.NET Framework 3.0
Gets a collection of cookies sent by the client.
Namespace: System.Web
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
/** @property */ public HttpCookieCollection get_Cookies ()
public function get Cookies () : HttpCookieCollection
Not applicable.
Property Value
An HttpCookieCollection object representing the client's cookie variables.ASP.NET includes two intrinsic cookie collections. The collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. The collection accessed through the Cookies collection of HttpResponse contains new cookies created on the server and transmitted to the client in the Set-Cookie header.
| Topic | Location |
|---|---|
| How to: Read a Cookie | Building ASP .NET Web Applications |
| How to: Read a Cookie | Building ASP .NET Web Applications |
The following code example loops through all cookies sent by the client and sends the name, expiration date, security parameter, and values of each cookie to the HTTP output.
int loop1, loop2;
HttpCookieCollection myCookieColl;
HttpCookie myCookie;
myCookieColl = get_Request().get_Cookies();
// Capture all cookie names into a string array.
String arr1[] = myCookieColl.get_AllKeys();
// Grab individual cookie objects by cookie name.
for (loop1 = 0; loop1 < arr1.length; loop1++) {
myCookie = myCookieColl.get_Item(arr1[loop1]);
get_Response().Write(("Cookie: " + myCookie.get_Name() + "<br>"));
get_Response().Write(("Expires: " + myCookie.get_Expires()
+ "<br>"));
get_Response().Write(("Secure:" + myCookie.get_Secure() + "<br>"));
//Grab all values for single cookie into an object array.
String arr2[] = myCookie.get_Values().get_AllKeys();
//Loop through cookie Value collection and print all values.
for (loop2 = 0; loop2 < arr2.length; loop2++) {
get_Response().Write(("Value" + loop2 + ": "
+ get_Server().HtmlEncode(arr2[loop2]) + "<br>"));
}
}
var arr1, arr2 : String[] var myCookieColl : HttpCookieCollection var myCookie : HttpCookie myCookieColl = Request.Cookies // Capture all cookie names into a string array. arr1 = myCookieColl.AllKeys // Grab individual cookie objects by cookie name for(var i=0; i < arr1.Length; i++){ myCookie = myCookieColl(arr1[i]) Response.Write("Cookie: " + myCookie.Name + "<br>") Response.Write("Expires: " + myCookie.Expires + "<br>") Response.Write ("Secure:" + myCookie.Secure + "<br>") // Grab all values for single cookie into an object array. arr2 = myCookie.Values.AllKeys // Loop through cookie Value collection and print all values. for(var j=0; j < arr2.Length; j++){ Response.Write("Value " + j + ": " + Server.HtmlEncode(arr2[j]) + "<br>") } }
Community Additions
ADD
Show: