ASP. NET operation cookie and pros and cons

Source: Internet
Author: User

Objective

The first time to write articles, but also please a lot of attention, write bad and wrong also please give us a lot of advice, thank you!

I. What is a cookie

  A Cookie is a small piece of textual information that accompanies a user request and the page is passed between the WEB server and the browser. A Cookie contains information that a WEB application can read every time a user accesses a site.

Two. Restrictions on cookies

Most browsers support a maximum of 4096 bytes of cookies. Because this limits the size of cookies, it is best to use cookies to store small amounts of data, or to store identifiers such as user IDs.

Three. Manipulating cookies

1 storage.

Mode one: response.cookies["key"].value= "value";

Mode two: HttpCookie cookie = new HttpCookie ("Key", "value");

Cookies. Expires = DateTime.Now.AddMinutes (30); Setting the expiration time, if not set, turns off browser cookie invalidation.

RESPONSE.COOKIES.ADD (cookie);

2 read.

Mode one: if (request.cookies["key"]!=null)

String str = request.cookies ("key"). Value;

Mode two: if (request.cookies["key"]!=null) {

String Str=request.cookies.get ("key");

}

Tip: There are also multi-valued cookie reads.

3 Deleting cookies

HttpCookie cookie = new HttpCookie ("key");
Cookies.  Expires=datetime.now.adddays (-30); Set its validity period to a date in the past. This expired cookie is deleted when the browser checks the validity period of the cookie.
RESPONSE.COOKIES.ADD (cookie);

four. The scope of the Cookie.

There are two ways to set the scope of a Cookie:

1 limit the scope of cookies to a folder on the server, which allows you to restrict cookies to an application on the site.

HttpCookie Appcookie = new HttpCookie ("key");
Appcookie.value = "value";
Appcookie.expires = DateTime.Now.AddDays (1);
Appcookie.path = "/filename";
RESPONSE.COOKIES.ADD (Appcookie);

Note: in some browsers, the path is case-sensitive. You cannot control how users type URLs in their browsers, but if your application relies on cookies related to a particular path, make sure that the URLs in all hyperlinks you create match the case of the Path property value.

2 sets the scope to a domain, which allows you to specify which subdomains in the domain can access cookies.

(1) By default, cookies are associated with a specific domain. (e.g. website: http//:www.yiming.com)

HttpCookie Appcookie = new HttpCookie ("key");
Appcookie.value = "value";
Appcookie.domain = "www.yiming.com"; By default www.yiming.com
RESPONSE.COOKIES.ADD (Appcookie);

(2) Thedomain attribute creates a Cookie that can be shared among multiple subdomains, as shown in the following example: (Multiple sites can be shared, such as: bba.yiming.com;kks.yiming.com;doq.yiming.com, etc.)

HttpCookie Appcookie = new HttpCookie ("key");
Appcookie.value = "value";
Appcookie.domain = ". Yiming.com";
RESPONSE.COOKIES.ADD (Appcookie);

Five. Advantages and disadvantages.

Advantages:
The use of cookies can persist user information compared to session and application objects. Cookies are stored on the client, while sessions and application are stored on the server side, so cookies can be stored for long periods. The Web application can authenticate users by obtaining a cookie from the client.
ASP. NET contains two cookie collections, accessed through the HttpRequest cookie collection, which is not a subclass of the page class, so the usage differs from the session and application, as compared to the advantages of their cookie:
1. Can configure Expiration time
2. Simple: A cookie is a lightweight, text-based structure that includes simple key-value pairs
3. Data persistence: Because it is saved to the client
4. No server resources: Because the local client is stored

Disadvantages:

1. Do not store sensitive information in cookies, such as usernames, passwords, credit card numbers, etc.

2. Encrypt the information (especially when it is stored in Chinese) by means of HTML encoding of the cookie content prior to the cookie operation.

3. The cookie property does not indicate whether cookies are enabled. It only indicates whether the current browser originally supported cookies.

4. The amount of data stored by cookies is limited, and most browsers support a maximum capacity of 4096 bytes, so do not use cookies to store large amounts of data.

ASP. NET operation cookie and pros and cons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.