Cookies in Asp. Net and Asp. NetCookies

Source: Internet
Author: User

Cookies in Asp. Net and Asp. NetCookies
What isCookies

There are many types of Cookies, such as website cookies, browser Cookies, and session cookies. Cookies are used to store the data of browsers and website accesses, and are one of the methods for connecting Web servers to clients. When a user accesses different sites, each site sends a cookie to the user's browser, which stores the cookie separately. In fact, cookies are short text files that are transmitted between the Web server and the browser during user requests and pages.

 

CookiesUsage

Cookies are often used for identity authentication, user session recognition, and shopping cart processing. Cookies can also be used to pass data from one webpage to another.

 

Is it safe to use cookies?

Is it secure to use cookies? There is no clear answer to this question. Cookies may be stolen by hackers to obtain the victim's network account. Cookies cannot carry viruses or install malware on host computers. However, they can use spyware to track users' online browsing activities.

Use cookies to create cookies

There are many ways to create cookies. I will outline some of the more common methods:

 

Method 1: Use HttpCookies
// Method 1: HttpCookie TestCookies = new HttpCookie ("TestCookies"); TestCookies. value = TextBox1.Text; TestCookies. expires = DateTime. now. addHours (1); Response. cookies. add (TestCookies );
Method 2: Use Response
// Method 2 Response. Cookies ["TestCookies"]. Value = TextBox1.Text; Response. Cookies ["TestCookies"]. Expires = DateTime. Now. AddDays (1 );
Method 3: write multiple values into one cookie
// Write multiple values into one cookie. cookies ["TestCookies"] ["AboutMe"] = TextBox1.Text; Response. cookies ["TestCookies"] ["SurName"] = "Wen"; Response. cookies ["TestCookies"] ["FirstName"] = "Jianfeng"; Response. cookies ["TestCookies"] ["Sex"] = "Boy"; Response. cookies ["TestCookies"] ["Work"] = "Programmer"; Response. cookies ["TestCookies"]. expires = DateTime. now. addDays (1 );
Read cookie

In the above code, I have used three methods to create a cookie. Therefore, it is necessary to obtain the following:

For Method 1:

     string test = Request.Cookies["TestCookies"].Value;

 

For Method 2:

     string test = Request.Cookies["TestCookies"].Value;

 

For method 3:

// Retrieve multiple values in the same cookie method string test; test = Request. cookies ["TestCookies"] ["AboutMe"]; test = test + ", name:" + Request. cookies ["TestCookies"] ["SurName"]; test = test + "" + Request. cookies ["TestCookies"] ["FirstName"]; test = test + ", Gender:" + Request. cookies ["TestCookies"] ["Sex"]; test = test + ", Occupation:" + Request. cookies ["TestCookies"] ["Work"];
Label1.Text = test;

 

Delete cookie

In the above code, I have used three methods to create and read cookies. Now let's see how the following code deletes cookies.

If (Request. Cookies ["TestCookies"]! = Null) {Response. cookies ["TestCookies"]. expires = DateTime. now. addDays (-1); // refresh the page Response. redirect ("TestPage. aspx ");}

 

Understand the HttpCookies class, which contains a set of all cookie values

We do not need to use any extra namespace. We only need to reference the HttpCookies class because this class is derived from the System. Web namespace (see the first method ).

 

The HttpCookie class has some common attributes:

    • Domain: obtain or set the Domain to which the Cookie is associated.
    • Expires: Get or set the Cookie expiration date and time.
    • HasKeys: gets a value that indicates whether the Cookie has a subkey.
    • Name: Get or set the Cookie Name.
    • Path: gets or sets the virtual Path to be transmitted with the current Cookie.
    • Secure: gets or sets a value that indicates whether to use Secure Sockets Layer (SSL) (that is, to transmit cookies only through HTTPS.
    • Value: gets or sets a single Cookie Value.
    • Values: obtains the set of key-value pairs contained in a single Cookie object.

 

Cookie restrictions

Cookies have the following restrictions:

1. The maximum cookie size is 4096 bytes;

2. Only 20 cookies can be stored and can be used on a single website. If there are more than 20 cookies, the browser will discard the old cookies (IE will increase each domain to 50 );

3. You can change the browser settings to use or disable cookies. Therefore, we recommend that you check the user's status and prompt the user to enable cookies.

 

Sometimes, the user disables cookies in the browser, and there is no relevant prompt in the browser to remind the user to enable cookies. In this case, you need to check the user's browser, go to the homepage of the website, and display the corresponding prompt, or redirect to the page with the prompt message to remind the user. The following code checks whether your browser supports cookies.

Protected void Page_Load (object sender, EventArgs e) {if (Request. browser. cookies) {// the browser supports cookies and continues coding ......} else {// the browser does not support cookies, so a prompt message is displayed or redirected to a new page for processing }}

 

 

I do not recommend storing sensitive information in cookies. If necessary, encrypt the information.

These cookies are used in asp.net. You are welcome to join us.

 

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.