A
cookie is a
small bit of text that accompanies requests and pages as they go between the Web
server
and
browser
.
The cookie contains information the Web application can read whenever the user visits the site.
Most browsers support cookies of up to 4096 bytes.
You can add
cookies
to the
Cookies collection
in a number of ways.
The following example shows two methods to
write cookies:
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
You can also store
multiple name-value pairs in a single cookie. Which are referred to as
subkeys.
For example, instead of creating two separate cookies named
userName and lastVisit
, you can create a single cookie named
userInfo
that has the
subkeys userName and lastVisit.
To create a cookie with subkeys, you can use a variation of the syntax for writing a single cookie.
The following example shows
two ways to write the same cookie, each with two subkeys:
Response.Cookies["userInfo"]["userName"] = "patrick";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
By default, all cookies for a site are
stored together on the client, and all cookies are sent to the server with any request to that site. In other words,
every page in a site gets all of the cookies for that site. However,
you can set the scope of cookies in two ways:
-
Limit the scope of cookies to a folder on the server, which allows you to limit cookies to an application on the site.
To limit cookies to a folder on the server, set the cookie's
Path
property, as in the following example:
HttpCookie appCookie = new HttpCookie("AppCookie");
appCookie.Value = "written " + DateTime.Now.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
appCookie.Path = "/Application1";
Response.Cookies.Add(appCookie);
-
Set scope to a domain, which allows you to specify which subdomains in a domain can access a cookie.
To limit cookies scope to a domain, set the cookie's
Domain
property, as in this example:
Response.Cookies["domain"].Value = DateTime.Now.ToString();
Response.Cookies["domain"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["domain"].Domain = "support.abc.com";
When the domain is set in this way, the cookie will be available
only to pages in the specified subdomain.
You can also use the
Domain
property to create a cookie that can be shared among multiple subdomains, as shown in the following example:
Response.Cookies["domain"].Value = DateTime.Now.ToString();
Response.Cookies["domain"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["domain"].Domain = "abc.com";
Reading Cookies
When a browser makes a request to the server, it sends the cookies for that server along with the request.
In ASP.NET applications, cookies can be read using the
HttpRequest
object, which is available as the
Request property of
Page
class.
The structure of the
HttpRequest
object is essentially the same as that of the
HttpResponse
object, so cookies can be read out of the
HttpRequest
object much the same way you wrote cookies into the
HttpResponse
object.
The following code example shows two ways to get the value of a cookie named username and display its value in a Label control:
if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
The following code example shows one way to get the value of a
subkey:
if(Request.Cookies["userInfo"] != null)
{
Label1.Text =
Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);
Label2.Text =
Server.HtmlEncode(Request.Cookies["userInfo"]["lastVisit"]);
}
Refer:
http://msdn.microsoft.com/en-us/library/aa289495(v=vs.71).aspx[
^] for more.