Manipulating a single cookie and cookie dictionary in C#

The storage format of a single cookie and cookie dictionary in the browser is as follows: As

you can see, a single cookie is stored in the form of a single key-value pair, and the values ​​of the cookie dictionary are connected by multiple ampersands.
A cookie dictionary is used when a single cookie holds multiple values.

The following is an example of the operation of a single cookie and a cookie dictionary:
1. A single cookie

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="设置单个cookie" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="获取单个cookie" />
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="修改单个cookie" />
        </div>
    </form>
</body>
</html>

 .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class CookieSingle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("xfk_sid")
            {
                Value = "xfk11111111",
                Expires = DateTime.Now.AddDays(1)
            };
            Response.Cookies.Add(cookie);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var objCookie = Request.Cookies["xfk_sid"];
            if (objCookie != null)
            {
                this.Label1.Text += objCookie.Value + "----";
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            HttpCookie c1 = Request.Cookies["xfk_sid"];
            c1.Value = "after1111111111";
            Response.Cookies.Add(c1);
        }
    }
}

 
2. cookie dictionary

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="设置cookie字典" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="获取cookie字典" />
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="修改cookie字典" />
        </div>
    </form>
</body>
</html>

 .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class CookieDict : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("xfk_sidDict");
            cookie.Values.Add("s1", "ssssssssss");
            cookie.Values.Add("s2", "iiiiiiiii");
            cookie.Values.Add("s3", "ddddddddddd");
            Response.Cookies.Add(cookie);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["xfk_sidDict"];
            if (cookie != null && cookie.HasKeys) {
                foreach (string item in cookie.Values)
                {
                    this.Label1.Text += "---" + cookie.Values[item];
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
             HttpCookie cookie = Request.Cookies["xfk_sidDict"];
             if (cookie != null && cookie.HasKeys) {
                 cookie.Values.Set("s1", "hahahahahah");
                 cookie.Values.Set("s3", "heiheiheiheihi");
                 Response.Cookies.Add(cookie);
             }
        }
    }
}

 

Reference: https://www.cnblogs.com/chenlihong-886/articles/6234535.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171717&siteId=291194637