Cookies in ASP. NET

Source: Internet
Author: User
I. Introduction to Cookies

Presumably, people who are engaged in. NET have ever heard of Cookies! (It is estimated that this situation is especially rare !~). In fact, Cookies also have many other names, such as HTTP Cookies, Web Cookies, Brower Cookies, and Session Cookies. They have different names, in fact, it refers to the same thing! ~. This is the case.

Cookies are mainly used to store a small amount of data (note that a small amount of data). When the server and the browser are not connected, Cookies automatically save the information of some visitors. In particular, cookies are a type that stores a small amount of data and are stored on clients, such as browsers.

For example, the following two images:

Initialize Cookie

The preceding two figures show how to use cookies.

Ii. Use scope and advantages of Cookies

Speaking of the Use scope of Cookies, we are all very familiar with such features as permissions, session usernames, shopping carts, and so on. These are common and common features in our normal development. Of course, more than that, you may also use it in other aspects.

We need to note that Cookies not only store data, but also transfer data from one page to another.

Advantages:

1. Easy to use

2. Protect the data sent by the browser (that is, there is a certain degree of encryption, but the intensity is not strong)

3. If cookie exists on several sites, the browser will customize the arrangement for them.

Iii. Cookies Security and Local disk location

For security, Cookies are obviously insecure. Because Cookies record traces of user operations, some hackers can steal or destroy data by obtaining user access permissions, here, Cookie has its unfriendly side. Cookies are not software, so they cannot be executed like executable files or carry viruses in users' cookies.

Although cookies contain insecure elements, we cannot deny the benefits that cookies bring to our developers. For example, How useful is it to log on to a website, save the user name, and then call it on each page?

Where is the Cookie on the local disk?

First, check "show all files and folders"

Then open the browser to the current user in C: \ Users \ [user name] \ AppData \ Roaming \ Microsoft \ Windows \ Cookies. Open the cookies folder as follows:

Iv. How to create and read Cookies

4.1 create through HTTPCookies

Create Cookies:

HttpCookie StudentCookies = new HttpCookie("StudentCookies");StudentCookies.Value = TextBox1.Text;StudentCookies.Expires = DateTime.Now.AddHours(1);Response.Cookies.Add(StudentCookies);

Read Cookies:

string roll = Request.Cookies["StudentCookies"].Value;

4.2 create directly through Response

Create Cookies:

Response.Cookies["StudentCookies"].Value = TextBox1.Text;Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);

Read Cookies:

string roll = Request.Cookies["StudentCookies"].Value;

4.3-valued Storage

Create Cookies:

Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text;Response.Cookies["StudentCookies"]["FirstName"] = "Abhimanyu";Response.Cookies["StudentCookies"]["MiddleName"] = "Kumar";Response.Cookies["StudentCookies"]["LastName"] = "Vatsa";Response.Cookies["StudentCookies"]["TotalMarks"] = "499";Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1); 

Read Cookies:

string roll;roll = Request.Cookies["StudentCookies"]["RollNumber"];roll = roll + " " + Request.Cookies["StudentCookies"]["FirstName"];roll = roll + " " + Request.Cookies["StudentCookies"]["MiddleName"];roll = roll + " " + Request.Cookies["StudentCookies"]["LastName"];roll = roll + " " + Request.Cookies["StudentCookies"]["TotalMarks"];Label1.Text = roll; 
5. How to delete Cookies

The essence of deleting a cookie is to modify its expiration time. The Code is as follows:

if (Request.Cookies["StudentCookies"] != null){    Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(-1);    Response.Redirect("Result.aspx");  //to refresh the page}
6. The HTTPCookie class contains a set of all cookies.

For HttpCookie, we do not need to provide additional namespaces because the HttpCookie class inherits from the System. Web namespace. Through HttpCookie, we do not need to use Response and Request when using cookies, such as 4.2 and 4.3.

The HttpCooki class has some attributes, which are well understood as follows:

  • Domain: Cookie domain
  • Expires: Cookie expiration date
  • HasKeys: Whether the Cookie has a subkey
  • Name: Cookie name
  • Path: Cookie Virtual Path
  • Secure: Whether to establish a secure connection
  • Value: Cookie value
  • Values: Multiple values
7. Notes on cookies

1. The cookie size is 4096 bytes.

2. A single website can have a maximum of 20 cookies. If you have more than 20 cookies, the website will automatically delete the old cookies.

3. End Users have the right to disable cookie use in browsers.

(My browser is firfox ):

In some cases, end users disable the cookie in the browser. More importantly, Some browsers do not have the cookie function. In this example, you need to check the cookie in advance during development. If the browser does not support cookies, you must perform other operations.

The following code is used to determine whether the current browser supports cookies:

Protected void Page_Load (object sender, EventArgs e) {if (Request. browser. cookies) {// Cookie supported} else {// cookies not supported // specified to the user-specified page }}

 

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.