读写cookie

//读cookie
	public void LoadFromCookie()
        {
            goujianceshihuanjing();
            HttpCookie cookie = HttpContext.Current.Request.Cookies["DnnMenuData"];
            if (cookie != null)
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(Base64StringToCookieValue(cookie.Value));
                    XmlNodeList xnlList= document.SelectNodes("//n[@key]");
                    foreach (XmlNode xnItem in xnlList) 
                    {
                        if (xnItem.Attributes["key"].Value == Request["tabid"].ToString()) 
                        {
                            showUrl = xnItem.Attributes["url"].Value;
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }

///写cookie	
public string GetPermissionMenuXml()
{
            string menuXml = "<root id=\"\"><n bcrumb=\"1\" selected=\"1\" id=\"4404\" key=\"4404\" txt=\"首页\" ca=\"3\" url=\"http://www.126.com\" /></root>";
            	    HttpContext current = HttpContext.Current;
            HttpCookie cookie = new HttpCookie("DnnMenuData");
            cookie.Value = Convert.ToBase64String(Encoding.UTF8.GetBytes(menuXml));
            cookie.Expires = DateTime.MaxValue;
            current.Response.Cookies.Add(cookie);        
}
// Methods
        private static string Base64StringToCookieValue(string v)
        {
            string str = v;
            try
            {
                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(v)))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (FormatException)
            {
            }
            return str;
        }

        private static string CookieValueToBase64String(string v)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(v));
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值