C # how to add, delete, modify, and query cookies

Source: Internet
Author: User

----------------------------------------------------- Operation -----------------------------------------------------------

1. Add cookies (using cookies for sso, saving and modifying user information will depend on cookies)

Copy codeThe Code is as follows: # 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:

Copy codeThe Code is as follows: # 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:

Copy codeThe Code is as follows: # 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)

Copy codeThe Code is as follows: # 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)

Copy codeThe Code is as follows: # 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.

Copy codeThe Code is as follows: # 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.

The method is correct. It has been verified by the project. Your opinion is incorrect. please correct me !!

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.