Cookie中文乱码问题

写入Cookie:             

     Response.Cookies["admin_id"].Value = item["id"];  //设置值
     Response.Cookies["admin_id"].Expires = DateTime.Now.AddDays(1); //设置cookie过期时间

读取Cookie:

          string account_id= Request.Cookies["admin_id"].Value; //读取值

 

上面的方法只能适用用英文和数字,如果包含中文,可能会乱码,如果用Cookie读取中文则需要通过编码进行解决:

 

 写入Cookie:          

          Response.Cookies["admin_account"].Value = HttpUtility.UrlEncode(item["real_name"], System.Text.Encoding.UTF8);
          Response.Cookies["admin_account"].Expires = DateTime.Now.AddDays(1);

 

读取Cookie:

          ViewBag.admin_account = HttpUtility.UrlDecode(Request.Cookies["admin_account"].Value, System.Text.Encoding.UTF8);

posted @ 2021-09-08 10:39  凉介-C  阅读(175)  评论(0编辑  收藏  举报