//读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));
}
读写cookie
最新推荐文章于 2022-06-01 00:08:00 发布