Share some small experience in cookie operations (add, delete, modify, and query)

Source: Internet
Author: User

 

 

A single sign on (SSO) was created some time ago, and Token was stored with cookies.

 

Cookies used for storing user information on websites

 

In the past, I thought there were only a few cookie operations, but there were still many "Cup events ".

 

Next we will share some simple operations on cookies.

 

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

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

# 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(Operations are inevitable in the program. Modification of user information will also modify cookies after the database is updated to display the modified information)

Method 1:

# 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:

# 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. To find the complete method, write it)

# 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 not all methods can be used to delete cookies)

Method 1: (the most common method to delete cookies)

# 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.

 

# 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 !!

 

 

 

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.