SoFunction
Updated on 2025-03-08

c# How to operate cookies (add, delete, modify, and check)

-----------------------------------------------------operate-----------------------------------------------------------

1. Add cookies (using cookies to do sso, user information will be saved and modified will depend on cookies)

Copy the codeThe code is as follows:

#region##Add cookies
    ///<summary>
/// Add cookies
    ///</summary>
    public void AddCookies()
    {

        HttpCookie cookies = new HttpCookie("Porschev");
        cookies["name"] = "Zhong Wei";
        cookies["sex"] = "1";
        = (20);
        (cookies);

    }
    #endregion


When adding cookies, no one will make any mistakes. . . Just add Expires

2. Modify cookies (It is inevitable to operate in the program. After modifying user information, cookies will also be modified after updating the database, for the purpose of modifying display)

Method 1:

Copy the codeThe code is as follows:

#region##Modify cookies
///<summary>
/// Modify cookies
///</summary>
public void ModCookies()
{

HttpCookie cookies = ["Porschev"];
cookies["name"] = "wilson Z";

}
#endregion


Create a new test page. . Then take the name in the cookies, the result is: wilson Z;

This is one of the "cup" events encountered in this project. When modifying cookies in this way, select the cookies named Porschev in the update method.

The value of name is Zhong Wei instead of wilson Z, and the page display is not correct (the reason for page cache has been ruled out, and the update has indeed not been successful)

Modification method one:

Copy the codeThe code is as follows:

#region##Modify cookies
    ///<summary>
/// Modify cookies
    ///</summary>
    public void ModCookies()
    {

        HttpCookie cookies = ["Porschev"];
        cookies["name"] = "wilson Z";
//Add the following sentence
        ["Porschev"].Expires = (-1); 

    }
    #endregion


Test the formula again, the result is correct, take out the name value as: wilson Z, and the page display is also correct

Understand by yourself: you must invalidate cookies with expiration time of 20 minutes before depositing

3. Get cookies (This is the easiest. In order to ensure the complete method, please write it on)

Copy the codeThe code is as follows:

#region##get cookies
    ///<summary>
/// Get cookies
    ///</summary>
    public void GetCookies()
    {

        HttpCookie cookies = ["Porschev"];
string name = cookies["name"];   //Fetch the corresponding value through key, and the same applies to multiple keys.

    }
    #endregion


4. Delete cookies (There are many ways to delete cookies online, but deletion is not all effective)

Method 1: (The most commonly used method to delete cookies)

Copy the codeThe code is as follows:

#region##Delete cookies
    ///<summary>
/// Delete cookies
    ///</summary>
    public void DelCookeis()
    {

        if (["Porschev"] != null)
        {
            HttpCookie cookies = new HttpCookie("Porschev");
            = (-1);
            (cookies);
        }        

    }
    #endregion


Testing the formula in the project, deleting some cookies failed, and it was speechless, so I changed the method

Copy the codeThe code is as follows:

#region##Delete cookies
    ///<summary>
/// Delete cookies
    ///</summary>
    public void DelCookeis()
    {

        foreach (string cookiename in  )
        {
            HttpCookie cookies = [cookiename];
            if (cookies != null)
            {
               = (-1);
               (cookies);
               (cookiename);
            }
        }   

    }
    #endregion


Test formula: Delete successfully

Understand by yourself: Method 1 can only delete cookies in the current Response. If you do not delete the client cookies, you can use the method of traversing cookies.

The method must be correct. After actual verification of the project, the view may not be correct. If there is any error in understanding, please correct me! !