How to store an object in a cookie?

后端 未结 10 1829
名媛妹妹
名媛妹妹 2020-12-15 08:49

While this is possible in C#: (User is a L2S class in this instance)

User user = // function to get user
Session[\"User\"] = user;

why this

10条回答
  •  无人及你
    2020-12-15 09:19

    to store an object in a cookie we have to convert it into stringified presentation (compressed or not) which is limited to 4kb. this example demonstrates how to keep a little "Buy" object in a cookies (save/prolong/reset/clear). instead of separate code lines i've used a Json to fill this object with some data.

    using System;
    using System.Collections.Generic;
    using System.Web;
    using Newtonsoft.Json;
    public class Customer
    {
        public int id;
        public string name;
    }
    public class Order
    {
        public int id;
        public decimal total;
        public Customer customer;
    }
    public class OrderItem
    {
        public int id;
        public string name;
        public decimal price;
    }
    public class Buy
    {
        public Order order;
        public List cart;
    }
    static readonly string cookieName = @"buy";
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!IsPostBack)
            Restore_Click(null, null);
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string buy = JsonConvert.SerializeObject(new
        {
            order = new
            {
                id = 1,
                total = 20.10,
                customer = new
                {
                    id = 1,
                    name = "Stackoverflow"
                }
            },
            cart = new[] {
                new {
                    id = 1 , 
                    name = "Stack",
                    price = 10.05 
                },
                new {
                    id = 2 , 
                    name = "Overflow",
                    price = 10.05 
                }
            }
        });
        HttpContext.Current.Response.Cookies.Add(
            new HttpCookie(cookieName, buy) {
                Expires = DateTime.Now.AddDays(7)
            }
        );
        StatusLabel.Text = "Saved";
    }
    protected void Prolong_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddDays(7);
            HttpContext.Current.Response.Cookies.Add(cookie);
            StatusLabel.Text = "Prolonged";
        }
        else StatusLabel.Text = "Not prolonged - expired";
    }
    protected void Restore_Click(object sender, EventArgs e)
    {
        Buy buy = null;
        HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
        if (cookie != null)
        {
            buy = JsonConvert.DeserializeObject(cookie.Value);
            StatusLabel.Text = "Restored";
        }
        else StatusLabel.Text = "Not restored - expired";
    }
    protected void ClearOut_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddMonths(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
            StatusLabel.Text = "Cleared out";
        }
        else StatusLabel.Text = "Not found - expired";
    }
    

提交回复
热议问题