How to store an object in a cookie?

后端 未结 10 1854
名媛妹妹
名媛妹妹 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:26

    you can try this:

    public void AddToCookie(SessionUser sessionUser)
        {
            var httpCookie = HttpContext.Current.Response.Cookies["SessionUser"];
            if (httpCookie != null)
            {
                httpCookie["ID"] = sessionUser.ID.ToString();
                httpCookie["Name"] = sessionUser.Name;
                httpCookie["Email"] = sessionUser.Email;
                httpCookie["Phone"] = sessionUser.Phone;
                httpCookie.Expires = DateTime.Now.AddDays(1);
            }
    
        }
    

提交回复
热议问题