C # cookie usage

Source: Internet
Author: User

Delete cookie
Response. Cookies ["cookiename"]. expires = datetime. Now. adddays (-1 );

Use C # To create and read cookies

Use C # To create and read cookies:

1. Create a cookie and write data:

// Create and write cookies

Httpcookie cookie = request. Cookies ["mws_user"];

If (cookie = NULL)

{

Cookie = new httpcookie ("mws_user ");

}

Cookie. Values. Set ("userid", struserid );

Response. setcookie (cookie );

 

Ii. Read COOKIE:

Httpcookie cookie = request. Cookies ["mws_user"];

If (cookie! = NULL & cookie ["userid"]. tostring ()! = "")

{

Response. Write ("Cookie =" + Cookie ["userid"]. tostring ());

}

 

1. Cookie directory

Cookie is stored in the client and stored in the "temporaryinternetfiles" directory. Therefore, there is a security issue. You can find the location in the following ways: Open the control panel → Internet Options → General → Temporary Internet Files → settings → you can see the "current location ", → click "View File" to open the folder directly. You can also click "Move folder" to change its location.

2. Cookie Validity Period

We can clearly see the "Deadline" (that is, the validity period) of each cookie document ). During the validity period, when the user administrator on the computer accesses 172.meibu.com again, ie will, together with the aforementioned name "Cookie: the cookie document content of the administrator@172.meibu.com is sent together to the server.

If this document contains the values of multiple cookies, the deadline is subject to the final expiration date.

3. Cookie type

Here we divide the score by the validity period, which is divided into two types:

A) instant

It means that after the browser is closed (all Internet Explorer that browses 172.meibu.com), the cookie becomes invalid and will not appear in the "temporaryinternetfiles" directory. In fact, it also has a deadline, for "0001-001"

B) durable type

Indicates the cookie that can be found in the "temporaryinternetfiles" directory after the specific "Deadline" has been specified.

4. Cookie content

Double-click Open COOKIE: administrator@172.meibu.com, and we see the following content, such as (http://blog.csdn.net/images/blog_csdn_net/cityhunter172/85935/r_aspxForm06.jpg ):

"■" Is a line break. If you want to break it, ask me how I know it. I would be happy to tell you: this is experience! From the moment I learned C #, I used notepad, the first Windows program, to open the knife and save the document.

So the server read the format such as (http://blog.csdn.net/images/blog_csdn_net/cityhunter172/85935/r_aspxForm07.jpg ):

5. Issue cookie on ASP. NET page

The. CS code for sending the cookie is:

System. Web. httpcookie ck = new httpcookie ("ckvalue0 ");

CK ["author"] = "cityhunter ";

CK. expires = system. datetime. Now. addminutes (10); // if not specified, the cookie is an instant cookie.

// Ck. path = "/formtest/managesys"; // set the virtual path of the cookie. Note that it must start with "/"; otherwise, the cookie is invalid; please take a look at its relationship with the "name" and "Internet address" of the cookie document at the room end.

Response. Cookies. Add (CK );

Ck = newhttpcookie ("ckvalue1"); // create a new cookie named ckvalue1

CK. expires = system. datetime. Now. addminutes (20); // expire 20 minutes later

CK ["e_mail"] = "mailto: cityhunter172@126.com % 22; // set the e_mail value in ckvalue1

CK ["personalweb"] = "172.meibu.com ";

Response. Cookies. Add (CK); // Add this cookie

6. Retrieve the issued cookie value

Response. Write (request. Cookies ["ckvalue0"] ["author"] + "<br>"); // you don't need to explain it.

Response. Write (request. Cookies ["ckvalue1"] ["e_mail"] + "<br> ");

Response. Write (request. Cookies ["ckvalue1"] ["personalweb"]);

I haven't finished my homework for a long time (Why ?), In this third article, I spent even two weeks in my spare time debugging, summarization, and writing. I said that time is as expensive as gold. I don't know how much money I can get for the time I spent? I don't expect any money. I can even get a comment from you. Remember, your comment is the motivation to continue writing.

Job: Assign the cookie to the following values and obtain the correct values.

CK ["str1"] = "2222 ";

CK ["str"] = "str0 = 11111 & str1 = 223 ";

Certainly, the request. cookies ["ckvalue1"] ["str"] cannot get the string "str0 = 11111 & str1 = 223". Please try the request. cookies ["ckvalue1"] ["str1"] Get unexpected strings.

Tip: Use server. urlencode () and server. urldecode ()

XIII. Issuance of permanent verification cookies

Finally ...... Finally ...... In the last chapter, let's look back and see 12 chapters. I did not expect that even when I was a young man, I could write thousands of words. I had to admire myself! Looking back, a huge film of fainting people ....... How far is it always? How long is it permanent? Only the day knows.

When you log on to csdn, do you notice a check box "do not need to log on again within 2 weeks"? How does this happen? Have you ever encountered the following confusion: When you run system. Web. Security. formsauthentication. setauthcookie, The createpersistentcookie is explicitly set to true. Why can't you directly access the website after you close the browser? Next we will explain this question and introduce how to manually create an authentication ticket and add it to the cookie.

System. Web. Security. formsauthenticationticket TK = Newsystem. Web. Security. formsauthenticationticket (

1, // specified version number: can be specified at will

"Admin", // logon Username: corresponding to <allowusers = "admin "... /> Users attribute

System. datetime. Now, // release time

System. datetime. Now. addyears (100), // expiration time: after January 1, 100, it will last long.

False, // whether the cookie is persistent: No use has been found, at least I do not know yet. The following describes

"Test user data" // user data: available (system. Web. Security. formsidentity) user. Identity). Ticket. userdata get

);

Stringstr = system. Web. Security. formsauthentication. Encrypt (TK); // encrypted identity verification

// Declare a cookie named <formsname = ". apsx "... /> Name attribute, corresponding to the string encrypted after identity verification

System. Web. httpcookieck = newhttpcookie (system. Web. Security. formsauthentication. formscookiename, STR );

// Specify the cookie as <formspath = "/"... /> Path attribute. If this parameter is not specified, the default value is "/".

CK. Path = system. Web. Security. formsauthentication. formscookiepath;

// This sentence is very important. If it is missing, even if the cookie is identified as a persistent cookie in identity authentication, it will only expire after the browser is closed. Therefore, I said: I really don't know how to specify persistent cookie for identity authentication.

CK. expires = system. datetime. Now. addyears (100 );

Response. Cookies. Add (CK); // Add to the room end

 

 

1. Cookie directory

Cookie is stored in the client and stored in the "temporaryinternetfiles" directory. Therefore, there is a security issue. You can find the location in the following ways: Open the control panel → Internet Options → General → Temporary Internet Files → settings → you can see the "current location ", → click "View File" to open the folder directly. You can also click "Move folder" to change its location.

2. Cookie Validity Period

We can clearly see the "Deadline" (that is, the validity period) of each cookie document ). During the validity period, when the user administrator on the computer accesses 172.meibu.com again, ie will, together with the aforementioned name "Cookie: the cookie document content of the administrator@172.meibu.com is sent together to the server.

If this document contains the values of multiple cookies, the deadline is subject to the final expiration date.

3. Cookie type

Here we divide the score by the validity period, which is divided into two types:

A) instant

It means that after the browser is closed (all Internet Explorer that browses 172.meibu.com), the cookie becomes invalid and will not appear in the "temporaryinternetfiles" directory. In fact, it also has a deadline, for "0001-001"

B) durable type

Indicates the cookie that can be found in the "temporaryinternetfiles" directory after the specific "Deadline" has been specified.

4. Cookie content

Double-click Open COOKIE: administrator@172.meibu.com, and we see the following content, such as (http://blog.csdn.net/images/blog_csdn_net/cityhunter172/85935/r_aspxForm06.jpg ):

"■" Is a line break. If you want to break it, ask me how I know it. I would be happy to tell you: this is experience! From the moment I learned C #, I used notepad, the first Windows program, to open the knife and save the document.

So the server read the format such as (http://blog.csdn.net/images/blog_csdn_net/cityhunter172/85935/r_aspxForm07.jpg ):

5. Issue cookie on ASP. NET page

The. CS code for sending the cookie is:

System. Web. httpcookie ck = new httpcookie ("ckvalue0 ");

CK ["author"] = "cityhunter ";

CK. expires = system. datetime. Now. addminutes (10); // if not specified, the cookie is an instant cookie.

// Ck. path = "/formtest/managesys"; // set the virtual path of the cookie. Note that it must start with "/"; otherwise, the cookie is invalid; please take a look at its relationship with the "name" and "Internet address" of the cookie document at the room end.

Response. Cookies. Add (CK );

Ck = newhttpcookie ("ckvalue1"); // create a new cookie named ckvalue1

CK. expires = system. datetime. Now. addminutes (20); // expire 20 minutes later

CK ["e_mail"] = "mailto: cityhunter172@126.com % 22; // set the e_mail value in ckvalue1

CK ["personalweb"] = "172.meibu.com ";

Response. Cookies. Add (CK); // Add this cookie

6. Retrieve the issued cookie value

Response. Write (request. Cookies ["ckvalue0"] ["author"] + "<br>"); // you don't need to explain it.

Response. Write (request. Cookies ["ckvalue1"] ["e_mail"] + "<br> ");

Response. Write (request. Cookies ["ckvalue1"] ["personalweb"]);

I haven't finished my homework for a long time (Why ?), In this third article, I spent even two weeks in my spare time debugging, summarization, and writing. I said that time is as expensive as gold. I don't know how much money I can get for the time I spent? I don't expect any money. I can even get a comment from you. Remember, your comment is the motivation to continue writing.

Job: Assign the cookie to the following values and obtain the correct values.

CK ["str1"] = "2222 ";

CK ["str"] = "str0 = 11111 & str1 = 223 ";

Certainly, the request. cookies ["ckvalue1"] ["str"] cannot get the string "str0 = 11111 & str1 = 223". Please try the request. cookies ["ckvalue1"] ["str1"] Get unexpected strings.

Tip: Use server. urlencode () and server. urldecode ()

XIII. Issuance of permanent verification cookies

Finally ...... Finally ...... In the last chapter, let's look back and see 12 chapters. I did not expect that even when I was a young man, I could write thousands of words. I had to admire myself! Looking back, a huge film of fainting people ....... How far is it always? How long is it permanent? Only the day knows.

When you log on to csdn, do you notice a check box "do not need to log on again within 2 weeks"? How does this happen? Have you ever encountered the following confusion: When you run system. Web. Security. formsauthentication. setauthcookie, The createpersistentcookie is explicitly set to true. Why can't you directly access the website after you close the browser? Next we will explain this question and introduce how to manually create an authentication ticket and add it to the cookie.

System. Web. Security. formsauthenticationticket TK = Newsystem. Web. Security. formsauthenticationticket (

1, // specified version number: can be specified at will

"Admin", // logon Username: corresponding to <allowusers = "admin "... /> Users attribute

System. datetime. Now, // release time

System. datetime. Now. addyears (100), // expiration time: after January 1, 100, it will last long.

False, // whether the cookie is persistent: No use has been found, at least I do not know yet. The following describes

"Test user data" // user data: available (system. Web. Security. formsidentity) user. Identity). Ticket. userdata get

);

Stringstr = system. Web. Security. formsauthentication. Encrypt (TK); // encrypted identity verification

// Declare a cookie named <formsname = ". apsx "... /> Name attribute, corresponding to the string encrypted after identity verification

System. Web. httpcookieck = newhttpcookie (system. Web. Security. formsauthentication. formscookiename, STR );

// Specify the cookie as <formspath = "/"... /> Path attribute. If this parameter is not specified, the default value is "/".

CK. Path = system. Web. Security. formsauthentication. formscookiepath;

// This sentence is very important. If it is missing, even if the cookie is identified as a persistent cookie in identity authentication, it will only expire after the browser is closed. Therefore, I said: I really don't know how to specify persistent cookie for identity authentication.

CK. expires = system. datetime. Now. addyears (100 );

Response. Cookies. Add (CK); // Add to the room end

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.