Posts Tagged asp cookie

Cookies In ASP.NET

Cookies provide a means in Web applications to store user-specific information, such as history or user preferences. A cookie is a small bit of text that accompanies requests and responses as they go between the Web server and client. The cookie contains information that the Web application can read whenever the user visits the site.

The browser manages the cookies on client computers. Cookies are sent to the client using the HttpResponse object, which exposes a property called Cookies. Any cookies that you want your Web application to send to the browser must be added to this collection. When you write a new cookie, you must specify the Name and Value. Each cookie must have a unique name so that your Web application can identify it when the browser sends it with future requests.

There are two ways to write a cookie to a user’s computer. You can either directly set cookie properties on the Cookies collection or you can create an instance of the HttpCookie object and add it to the Cookies collection. You must create cookies before the ASP.NET page is rendered to the client. For example, you can write a cookie in a Page_Load event handler but not in a Page_Unload event handler.

To write a cookie by setting cookie properties on the Cookies collection:

The following code example shows a cookie named UserSettings with the values of the subkeys Font and Color set. It also sets the expiration time to be tomorrow.
Response.Cookies[“UserSettings”][“Font”] = “Arial”;
Response.Cookies[“UserSettings”][“Color”] = “Blue”;
Response.Cookies[“UserSettings”].Expires = DateTime.Now.AddDays(1d);

To write a cookie by creating an instance of the HttpCookie object:
HttpCookie myCookie = new HttpCookie(“UserSettings”);
myCookie[“Font”] = “Arial”;
myCookie[“Color”] = “Blue”;
myCookie.Expires = DateTime.Now.AddDays(1d);

More Examples:
Cookies with One Value:
Response.Cookies[“userName”].Value = “Bikash”;
Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie(“lastVisit”);
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);

Cookies with More Than One Value:
Response.Cookies[“userInfo”][“userName”] = “Bikash”;
Response.Cookies[“userInfo”][“lastVisit”] = DateTime.Now.ToString();
Response.Cookies[“userInfo”].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie(“userInfo”);
aCookie.Values[“userName”] = “Bikash”;
aCookie.Values[“lastVisit”] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);

By default, cookies are shared by all pages that are in the same domain, but you can limit cookies to specific subfolders in a Web site by setting their Path property. To allow a cookie to be retrieved by all pages in all folders of your application, set it from a page that is in the root folder of your application and do not set the Path property.

If you do not specify an expiration limit for the cookie, the cookie is not persisted to the client computer and it expires when the user session expires.

Cookies can store values only of type String. You must convert any non-string values to strings before you can store them in a cookie. For many data types, calling the ToString method is sufficient.

More Information:
Most browsers support cookies of up to 4096 bytes.

Browsers also impose limitations on how many cookies your site can store on the user’s
computer. Most browsers allow only 20 cookies per site; if you try to store more,
the oldest cookies are discarded. Some browsers also put an absolute limit, usually
300, on the number of cookies they will accept from all sites combined.

In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on),
plus the length of the value that you store in it, all of which counts toward the 4096-byte limit.
If you store five subkeys instead of five separate cookies, you save the overhead of the separate
cookies and can save around 200 bytes.

Thanks to all
For any information please Mail me:

, , , , , , ,

Leave a comment