Protected void cookieuse ()
{
// Write cookie
Httpcookie cookie = new httpcookie ("info"); // defines the cookie object and the item named info.
Datetime dt = datetime. Now; // defines the time object
Timespan Ts = new timespan (,); // the effective time of the cookie. For details, refer to the msdn
Cookie. expires = DT. Add (TS); // Add time
Cookie. Values. Add ("user", "cxbkkk"); // Add attributes
Cookie. Values. Add ("userid", "1203 ");
Response. appendcookie (cookie); // confirm that the cookie is being written.
// Read cookie
If (request. Cookies ["info"]! = NULL)
{
String temp = convert. tostring (request. cookies ["info"]. values ["user"]) + "" + convert. tostring (request. cookies ["info"]. values ["userid"]);
// Use request. Cookies ["info"]. Value to read all data)
If (temp = "")
{
Response. Write ("null ");
}
Else
Response. Write (temp );
}
Else
{
Response. Write ("error ");
}
// Modify the cookie
Response. Cookies ["info"] ["user"] = "2 ";
Response. Cookies ["info"]. expires = datetime. Now. adddays (1); // Delete the attributes under the cookie
Httpcookie acookie = request. Cookies ["info"];
Acookie. Values. Remove ("userid ");
Acookie. expires = datetime. Now. adddays (1 );
Response. Cookies. Add (acookie); // delete all cookies, that is, set the expiration time to now.
Int Limit = request. Cookies. Count-1;
For (INT I = 0; I <limit; I ++)
{
Acookie = request. Cookies;
Acookie. expires = datetime. Now. adddays (-1 );
Response. Cookies. Add (acookie );
}
}
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, as shown in (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 took the first WindowsProgram-- Notepad to open the knife, the experience gained when saving the document.
The format read by the server is as follows (Http://blog.csdn.net/images/blog_csdn_net/cityhunter172/85935/r_aspxForm07.jpg):
5. Issue cookie on ASP. NET page
The. CSCodeIs:
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 to finish writing when I was young.CompositionEven, you can write thousands of words.ArticleCome on, I have 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