A cookie is defined in the HTTP header. Use the HttpResponse class to send a cookie to the client.Response is a property of the Page class, which returns an object of type HttpResponse. The HttpResponse class defines the Cookies property, which returns an HttpCookieCollection. Multiple cookies can be returned to the client with the HttpCookieCollection.The following sample code shows how a cookie can be sent to the client.
First, an HttpCookie object is instantiated. In the constructor of this class, the name of the cookie is set — here it is "mycookie". The HttpCookie class has a Values property to add multiple cookie values. If you just have one cookie value to return, you can use the Value property instead. However, if you plan to send multiple cookie values, it is better to add the values to a single cookie instead of using multiple cookies.
Cookies can be temporary and valid within a browser session, or they can be stored on the client disk.To make the cookie permanent, the Expires property must be set with the HttpCookie object. With the Expires property, a date defines when the cookie is not valid anymore; in the following example, it is set to a date three months from the current date.
Although a specific date can be set, there is no guarantee that the cookie is stored until the date is reached. The user can delete the cookie, and the browser application deletes the cookie if too many cookies are stored locally. When the client requests a page from the server, and a cookie for this server is available on the client,the cookie is sent to the server as part of the HTTP request. Reading the cookie in the ASP.NET page can be achieved by accessing the cookies collection in the HttpRequest object.
Similarly to the HTTP response, the Page class has a Request property that returns an object of type HttpRequest. The property Cookies returns an HttpCookieCollection that can be used to read the cookies sent by the client. A cookie can be accessed by its name with the indexer, and then the Values property of the HttpCookie is used to get the value from the cookie:
ASP.NET makes it easy to use cookies, but you must be aware of the cookie’s restrictions. Recall that a browser accepts just 20 cookies from a single server and 300 cookies for all servers. In addition, a cookie cannot store more than 4K of data. These restrictions ensure that the client disk won’t be filled with cookies.
First, an HttpCookie object is instantiated. In the constructor of this class, the name of the cookie is set — here it is "mycookie". The HttpCookie class has a Values property to add multiple cookie values. If you just have one cookie value to return, you can use the Value property instead. However, if you plan to send multiple cookie values, it is better to add the values to a single cookie instead of using multiple cookies.
string myval = "Test";
var cookie = new HttpCookie("mycookie");
cookie.Values.Add("mystate",
myval);
Response.Cookies.Add(cookie);
Cookies can be temporary and valid within a browser session, or they can be stored on the client disk.To make the cookie permanent, the Expires property must be set with the HttpCookie object. With the Expires property, a date defines when the cookie is not valid anymore; in the following example, it is set to a date three months from the current date.
var cookie = new
HttpCookie("mycookie");
cookie.Values.Add("mystate",
"Test");
cookie.Expires = DateTime.Now.AddMonths(3);
Response.Cookies.Add(cookie);
Although a specific date can be set, there is no guarantee that the cookie is stored until the date is reached. The user can delete the cookie, and the browser application deletes the cookie if too many cookies are stored locally. When the client requests a page from the server, and a cookie for this server is available on the client,the cookie is sent to the server as part of the HTTP request. Reading the cookie in the ASP.NET page can be achieved by accessing the cookies collection in the HttpRequest object.
Similarly to the HTTP response, the Page class has a Request property that returns an object of type HttpRequest. The property Cookies returns an HttpCookieCollection that can be used to read the cookies sent by the client. A cookie can be accessed by its name with the indexer, and then the Values property of the HttpCookie is used to get the value from the cookie:
HttpCookie cookie = Request.Cookies["mycookie"];
string myval = cookie.Values["mystate"];
ASP.NET makes it easy to use cookies, but you must be aware of the cookie’s restrictions. Recall that a browser accepts just 20 cookies from a single server and 300 cookies for all servers. In addition, a cookie cannot store more than 4K of data. These restrictions ensure that the client disk won’t be filled with cookies.
No comments:
Post a Comment