Next we will share some simple operations on cookies.
1. Add cookies (using cookies for sso, saving and modifying user information will depend on cookies)
| The code is as follows: |
Copy code |
# Region # Add cookeis /// <Summary> /// Add cookeis /// </Summary> Public void AddCookies () { HttpCookie cookies = new HttpCookie ("Porschev "); Cookies ["name"] = "Zhong Wei "; Cookies ["sex"] = "1 "; Cookies. Expires = DateTime. Now. AddMinutes (20 ); Response. Cookies. Add (cookies ); } # Endregion |
Generally, no error occurs when you add cookies... Remember to add Expires.
2. Modify cookies (it is inevitable that cookies will be modified in the program, and the user information will be modified after the database is updated. In order to display the modified cookies)
Method 1:
| The code is as follows: |
Copy code |
# Region # modify cookies /// <Summary> /// Modify cookies /// </Summary> Public void ModCookies () { HttpCookie cookies = Request. Cookies ["Porschev"]; Cookies ["name"] = "wilson Z "; } # Endregion |
Create a test page .. Then retrieve the name in the cookie. Result: wilson Z;
This is one of the "Cups" in this project. When cookies are modified in this way, the cookie named Porschev is retrieved in the update method,
The value of name is still Zhong Wei, not wilson Z, and the page is not displayed (the reason for page cache has been ruled out, and the update is indeed not successful)
Method 1:
| The code is as follows: |
Copy code |
# Region # modify cookies /// <Summary> /// Modify cookies /// </Summary> Public void ModCookies () { HttpCookie cookies = Request. Cookies ["Porschev"]; Cookies ["name"] = "wilson Z "; // Add the following sentence Response. Cookies ["Porschev"]. Expires = DateTime. Now. AddMinutes (-1 ); } # Endregion |
Again, the result is correct. The name value is wilson Z, and the page is displayed correctly.
You understand: you must invalidate cookies that have been previously stored for 20 minutes.
3. Get cookies (this is the simplest method. Write it for the complete method)
| The code is as follows: |
Copy code |
# Region # obtain cookies /// <Summary> /// Obtain cookies /// </Summary> Public void GetCookies () { HttpCookie cookies = Request. Cookies ["Porschev"]; String name = cookies ["name"]; // obtain the corresponding value using the key. Multiple keys are also used. } # Endregion |
4. Delete cookies (there are many ways to delete cookies on the Internet, but deletion does not work)
Method 1: (the most common method to delete cookies)
| The code is as follows: |
Copy code |
# Region # delete cookies /// <Summary> /// Delete cookies /// </Summary> Public void DelCookeis () { If (Request. Cookies ["Porschev"]! = Null) { HttpCookie cookies = new HttpCookie ("Porschev "); Cookies. Expires = DateTime. Now. AddDays (-1 ); Response. Cookies. Add (cookies ); } } # Endregion |
As tested in the project, some cookies failed to be deleted and were speechless.
| The code is as follows: |
Copy code |
# Region # delete cookies /// <Summary> /// Delete cookies /// </Summary> Public void DelCookeis () { Foreach (string cookiename in Request. Cookies. AllKeys) { HttpCookie cookies = Request. Cookies [cookiename]; If (cookies! = Null) { Cookies. Expires = DateTime. Today. AddDays (-1 ); Response. Cookies. Add (cookies ); Request. Cookies. Remove (cookiename ); } } } # Endregion |
Test Formula: deletion successful
You understand: Method 1 can only delete the cookies in the current Response. If you do not delete the cookies, you can traverse the cookies.