Use ASP. NET cookies and ASP. netcookies

Source: Internet
Author: User

Use ASP. NET cookies and ASP. netcookies

First, let's take a look at the cookie learning diagram. What do we learn?

I. What is a Cookie?

A Cookie is a short text message that contains a text file of no more than 4 kb on the client's hard disk.
When user requests and pages are transmitted between the Web server and the browser
Each time a user accesses a site, the Web application can read the information contained in the Cookie.

Ii. Working Principle

Because HTTP is a stateless protocol, the server cannot know the customer's identity from the network connection. What should we do? Just give the client a pass. Each person must carry his/her own pass no matter who visits the client. In this way, the server can confirm the customer's identity from the pass. This is how cookies work.
Cookie is actually a short text message. The client requests the server. If the server needs to record the user status, use response to issue a Cookie to the client browser. The client browser saves the Cookie. When the browser requests the website again, the browser submits the requested URL together with the Cookie to the server. The server checks the Cookie to identify the user status. The server can also modify the Cookie content as needed.

 

3. attributes and methods of Cookie objects
Attribute:

(1). Name: Get or set the Cookie Name
(2). Value: Get or set the Cookie Value
(3). Expires: Get or set the Cookie expiration time
(4). Version: Get or set the Version that meets the HTTP maintenance status of the Cookie.

Method:
(1). Add: Add a Cookie variable to save the specified cookie to the Cookies set.
(2). Clear: Clear the variables in the Cookie set.
(3). Get: Get the Cookie variable value through the variable name or index.
(4). Remove: delete a Cookie object using the Cookie variable name or index.

Iv. Advantages and Disadvantages

Advantages:
Compared with Session and Application objects, Cookie can be used to persistently store user information. The Cookie is stored on the client, while the Session and Application are stored on the server. Therefore, the Cookie can be saved for a long time. Web applications can obtain client cookies for user identity authentication.
Asp.net contains two Cookie sets that are accessed through HttpRequest's Cookie set. Cookie is not a subclass of the Page class. Therefore, the usage method is different from Session and Application, which has the following advantages over Cookie:
1. You can configure the expiration time.
2. Simple: Cookie is a lightweight text-based structure, including simple key-value pairs
3. Data Persistence: because the data is saved to the client
4. No server resources: because it is stored on the local client

DisadvantagesAs follows:
1. size limit:
2. Uncertainty: users may delete or disable cookies.
3. Security Risks: forge and modify

5. What should I pay attention?
[1]. When you use cookies to save the request information sent from the client browser to the server page, the length of time it takes depends on the Expires attribute of the Cookie object and can be set as needed. If no Cookie expiration date is set, they are saved only until the browser is closed. If you set the Expires attribute of the Cookie object to DateTime. MaxValue, the Cookie will never expire.

[2]. The amount of data stored in cookies is limited. Most browsers support a maximum capacity of 4096 bytes. Therefore, do not use cookies to store large amounts of data.

[3]. Not all browsers support cookies and data is stored in plain text on the client computer. Therefore, it is best not to use cookies to store sensitive unencrypted data.

[4]. in ASP. NET has two Cookies, namely, the Cookie set of the Response object and the Cookie set of the Request object. However, the two have different functions. The former can write the Cookie to the client, the latter can be used to read the Cookies stored on the client.
Next we will talk about the practical application of cookies.

First, you can log on to a website through the interface. The specific requirements are as follows:

  • After the first login, the login information is written into the Cookie of the user's computer;
  • When you log on again, read and display the Cookie information on your computer for the user to choose;
  • You can directly log on to the website by reading the information in the Cookie.

Procedure

1. Create a blank asp.net application in VS, add a Web form in the project manager, and use a two-row and three-column table for layout. After the layout is complete, place the two Label controls on the left of the layout table, set the Text attribute to the user name and password, and add two TextBox controls, one Button control, and one CheckBox control to the layout table, set the Text attribute of the Button control to "Logon", and set the Text attribute of the CheckBox control to "Remember user name and password ".

2. Add an HTML page

Change the ingress name to login.html

3. Write event processing code

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> protected void button#click (object sender, EventArgs e) {if (CheckBox1.Checked) {Response. cookies ["ID"]. expires = new DateTime (2016, 2, 24); // use the Cookie key ID to set the Response time. cookies ["PW"]. expires = new DateTime (2016, 2, 24); Response. cookies ["ID"]. value = TextBox1.Text; // set the Cookie Value with the key name ID to the text box content Response. cookies ["PW"]. value = TextBox2.Text;} Response. redirect ("Login.html") ;}</span>

First, determine whether the Cookie defined on the page is empty. If it is not empty, read the content in the Cookie and place it in TextBox1 and TextBox2 text boxes, in this way, when you log on to the same page for the second time on your machine, you can click the logon button to save the process of re-entering the user ID and password.

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> protected void Page_Load (object sender, EventArgs e) {if (Request. cookies ["ID"]! = Null & Request. Cookies ["PW"]! = Null) {TextBox1.Text = Request. cookies ["ID"]. value. toString (); TextBox2.Text = Request. cookies ["PW"]. value. toString (); // read the Cookie value with the key name ID and display it in TextBox2. }}</span>

As shown in.

 

The above is just a simple demonstration of several common features (retaining user personal information, such as name and password, to help users quickly log on to the web page), there are many unknown features for us to explore
For example:

  • Stores users' personal interests. Designers can set the website style based on the user interests recorded in cookies;
  • Records the information of the purchased items during online shopping;
  • Record the pop-up status of the pop-up window. When some pages are opened, a notification or advertisement window will pop up. You can use the Cookie record window to check whether the pop-up window has been popped up, if the dialog box appears, the dialog box is no longer displayed when you open the page again.

By using this example, I have learned more about how to use cookies. I have never read any of these similar articles before, and I feel that it is rare, however, it is not difficult to understand it after you try it. Therefore, do not be afraid of difficulties in learning.

The above is all the content of this article. I hope it will help you learn and make common progress!

Articles you may be interested in:
  • Asp.net: Two Methods for clearing cookies
  • Asp.net cookie clearing code
  • Asp.net cookie operations, writes, reads, and operations
  • Asp.net (C #) Cross-origin and cross-origin Cookie writing
  • Introduction to cookie usage in asp.net
  • Asp.net
  • Asp.net built-in object Cookies (Introduction/attribute methods/basic operations and instances)
  • Asp.net Cookie value Chinese garbled Problem Solution
  • Use cookie and md5 encryption in asp.net to implement the Code for remembering the password Function

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.