Asp. Basic knowledge of cookie programming in net (3)

Source: Internet
Author: User
Tags contains datetime range valid root directory
asp.net|cookie| program to view your cookies

You may find it helpful to understand the effect of creating cookies. It's easier to look at cookies because they are text files, and the key is that you can find them. Different browsers save cookies in different ways. I'll explain how Internet Explorer saves cookies. If you are using a different browser, check the browser's Help for information about Cookie processing.

An easy way to view cookies is to have Internet Explorer find them for you. In Internet Explorer, from the Tools menu, select Internet Options, click Settings on the General tab, and then click View Files. Internet Explorer opens a window that displays all of the temporary files, including cookies. Find a file in the window that starts with "Cookie:" or find a text file. Double-click a Cookie to open it in the default text file.

You can also turn on cookies by looking for a text file for a cookie on your hard disk. Internet Explorer saves the site's cookies in a file with a filename format of <user>@<domain>.txt, where <user> is your account name. For example, if your name is Mikepope and the site you visit is www.contoso.com, then the Cookie for that site will be saved in a file named Mikepope@www.contoso.txt. (The file name may contain a sequential number, such as Mikepope@www.contoso[1].txt.) )

This Cookie text file is user-related and will be saved according to the account. For example, in Windows XP, you can find a Cookie file in the directory shown below:

C:\Documents and Settings\<user>\cookies

To find the most recently created Cookie, you can sort the contents of the catalog by modified date and find the most recent modified file.

You can use a text editor to open cookies. If the file contains more than one cookie, the cookies are separated by an asterisk (*). The first line of each cookie is the name of the cookie, the second row is the value, and the remaining rows contain the daily processing information for the cookie, such as the expiration date and time. There is also a simple checksum in the cookie, and if you change the cookie name or the length of the value, the browser detects the modification and deletes the cookie.

Multi-value cookies (subkeys)

The above example uses a Cookie for each value to be saved (user name, last access time). You can also save multiple name/value pairs in a Cookie. Name/value pairs are also known as "Keys" or "subkeys," depending on what you read. (If you are familiar with the structure of the URL, you will find that the subkey is very much like the query string in it.) For example, if you do not want to create two separate cookies named "UserName" and "lastvisit", you can create a cookie named "UserInfo" that contains two subkeys: "UserName" and "lastvisit".

There are a number of reasons why we can use subkeys instead of individual cookies. Most obviously, it is more organized to put relevant or similar information in a Cookie. In addition, because all information is in a cookie, the cookie attribute such as the expiration date applies to all information. (Of course, if you want to specify different expiration dates for different types of information, you should keep the information in a separate Cookie.) )

Cookies with subkeys can also help you reduce the size of cookies. The total size of the cookie is limited to 4096 bytes and cannot hold more than 20 cookies for a Web site, as described in the previous cookie restriction section. With a single cookie with a button, the number of cookies in the site is no more than 20 limits. In addition, a Cookie consumes approximately 50 characters of the basic space cost (for saving the expiration information, and so on), plus the length of the value in which it is saved, the sum of which is close to 4K. If you use a five-child key instead of five separate cookies, you can save the basic space overhead of four cookies, saving about 200 bytes in total.

To create a cookie with a key, you can use the various syntaxes used to write a single cookie. The following example shows two different ways to write the same cookie with two subkeys per cookie:

Response.Cookies ("UserInfo") ("userName") = "Mike"
Response.Cookies ("UserInfo") ("lastvisit") = DateTime.Now.ToString
Response.Cookies ("UserInfo"). Expires = DateTime.Now.AddDays (1)

Dim Acookie as New HttpCookie ("UserInfo")
Acookie.values ("userName") = "Mike"
Acookie.values ("lastvisit") = DateTime.Now.ToString
Acookie.expires = DateTime.Now.AddDays (1)
RESPONSE.COOKIES.ADD (Acookie)

Control Cookie Valid Range

By default, all cookies for a site are saved together on the client, and all of the cookies are sent to the server along with the requests sent to the site, which means that every page of the site gets all the cookies for that site. Sometimes, however, you may want the cookie to be more targeted, and you can set the valid range of cookies in two ways:

Limit the valid scope of a cookie to a folder on the server, which in effect restricts the cookie to an application on the site.

Set the valid scope to a domain, allowing you to specify which subdomains in the domain can access cookies.

Restricting cookies to a folder or application

To restrict cookies to a folder on the server, set the Path property of the cookie as follows:

Dim Appcookie as New HttpCookie ("Appcookie")
Appcookie.value = "written" & Now.tostring
Appcookie.expires = now.adddays (1)
Appcookie.path = "/application1"
RESPONSE.COOKIES.ADD (Appcookie)

Of course, you can also write cookies by setting Response.Cookies directly, as described earlier in this article.

The path can be either a physical path under the site root or a virtual root directory. As a result, cookies can only be used in Application1 folders or pages in the virtual root directory. For example, if your site is named www.contoso.com, the Cookie that is generated in the previous example can only be used for pages with http://www.contoso.com/Application1/paths and all pages under that folder. It does not apply to pages in other applications, such as http://www.contoso.com/Application2/or http://www.contoso.com/.

Tip: Testing the Internet Explorer and Mozilla browsers reveals that the path used here is case-sensitive. In general, URLs on Windows servers are case-insensitive, but this is an exception. You cannot control how users enter URLs in a browser, but if your application relies on cookies that are related to a particular path, make sure that the URLs in all the hyperlinks you create match the case of the Path property value.

Limit the valid scope of a Cookie to a domain

By default, a Cookie is associated with a specific domain. For example, if your site is www.contoso.com, the cookies you write are sent to the server when a user requests a page from that site. (except for cookies with a specific path value, which I just explained in the previous section.) If your site has subdomains (such as contoso.com, sales.contoso.com, and support.contoso.com), you can associate cookies with specific subdomains. To do this, you need to set the Domain property of the Cookie, as follows:

Response.Cookies ("Domain"). Value = DateTime.Now.ToString
Response.Cookies ("Domain"). Expires = DateTime.Now.AddDays (1)
Response.Cookies ("Domain"). Domain = "Support.contoso.com"

If you set the domain in this way, the Cookie can only be used to specify pages in the child domain.

You can also use the Domain property to create cookies that can be shared across multiple child domains. For example, set the fields as follows:

Response.Cookies ("Domain"). Value = DateTime.Now.ToString
Response.Cookies ("Domain"). Expires = DateTime.Now.AddDays (1)
Response.Cookies ("Domain"). Domain = "contoso.com"

This allows the Cookie to be used for the primary domain, sales.contoso.com, and support.contoso.com.


Related Article

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.