SoFunction
Updated on 2025-03-07

Example of C# and Net Access Cookies Operation

Creation of cookies:

Create a username cookies on the client, whose value is oneday, and its validity period is 1 day.

Method 1:

Copy the codeThe code is as follows:

["username"].Value="admin";
["username"].Expires=(1);

Method 2:

Copy the codeThe code is as follows:

newcookie=new HttpCookie("username");
="oneday";
=(1);
(newcookie);


Create cookies with subkeys:

Copy the codeThe code is as follows:

newcookie=new HttpCookie("user");
["username"]="admin";
["password"]="admin";
=(1);
(newcookie);

Reading of cookies:

Sub-free key reading:

Copy the codeThe code is as follows:

if(["username"]!=null)
{
((["username"].Value));
}

There are subkeys to read:

Copy the codeThe code is as follows:

if(["user"]!=null)
{
((["user"]["username"].Value));
((["user"]["password"].Value));

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;


public class Cookie
{
    /// <summary>
/// Cookies assignment
    /// </summary>
/// <param name="strName">Primary key</param>
/// <param name="strValue">key value</param>
/// <param name="strDay">Valid days</param>
    /// <returns></returns>
    public bool setCookie(string strName, string strValue, int strDay)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
// = ".";// When you want to access across domain names, just specify the domain name for the cookie, the format is.
            = (strDay);
            = strValue;
            (Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
/// Read Cookies
    /// </summary>
/// <param name="strName">Primary key</param>
    /// <returns></returns>
    public string getCookie(string strName)
    {
        HttpCookie Cookie = [strName];
        if (Cookie != null)
        {
            return ();
        }
        else
        {
            return null;
        }
    }

    /// <summary>
/// Delete Cookies
    /// </summary>
/// <param name="strName">Primary key</param>
    /// <returns></returns>
    public bool delCookie(string strName)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
// = ".";// When you want to access across domain names, just specify the domain name for the cookie, the format is.
            = (-1);
            (Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}



Example:
Copy the codeThe code is as follows:

Cookie Cookie = new Cookie();
("name", "aaa",1);//Assign
("name");//Get the value
("name");//Delete

Note: When a cookie is stored in Chinese, it will be encoded in Chinese when stored, such as ("name", ("aaa"), 1), and decode it when reading.

In addition: As long as the cookie is not set to expire, the cookie will automatically become invalid when the browser is closed

Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

public class Cookie
{
    /// <summary>
/// Cookies assignment
    /// </summary>
/// <param name="strName">Primary key</param>
/// <param name="strValue">key value</param>
/// <param name="strDay">Valid days</param>
    /// <returns></returns>
    public bool setCookie(string strName, string strValue, int strDay)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
// = ".";// When you want to access across domain names, just specify the domain name for the cookie, the format is.
            = (strDay);
            = strValue;
            (Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
/// Read Cookies
    /// </summary>
/// <param name="strName">Primary key</param>
    /// <returns></returns>
    public string getCookie(string strName)
    {
        HttpCookie Cookie = [strName];
        if (Cookie != null)
        {
            return ();
        }
        else
        {
            return null;
        }
    }

    /// <summary>
/// Delete Cookies
    /// </summary>
/// <param name="strName">Primary key</param>
    /// <returns></returns>
    public bool delCookie(string strName)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
// = ".";// When you want to access across domain names, just specify the domain name for the cookie, the format is.
            = (-1);
            (Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}

Example:

Copy the codeThe code is as follows:

Cookie Cookie = new Cookie();
("name", "aaa",1);//Assign
("name");//Get the value
("name");//Delete

Note: When a cookie is stored in Chinese, it will be encoded in Chinese when stored, such as ("name", ("aaa"), 1), and decode it when reading.

In addition: As long as the cookie is not set to expire, the cookie will automatically become invalid when the browser is closed