public static void setUserStat(Model.UserStat model)
{
HttpCookie cookie = new HttpCookie("bloghu");
cookie.Expires = DateTime.Now.AddHours(1);
cookie.Value = Serialize(model);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
public static Model.UserStat getCurrentUserStat()
{
if (System.Web.HttpContext.Current.Request.Cookies["bloghu"] != null)
return (Model.UserStat)Deserialize(System.Web.HttpContext.Current.Request.Cookies["bloghu"].Value);
else
return new Model.UserStat();
}
static string Serialize(object obj)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
return Convert.ToBase64String(stream.ToArray());
}
static object Deserialize(string str)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
return formatter.Deserialize(stream);
}
{
HttpCookie cookie = new HttpCookie("bloghu");
cookie.Expires = DateTime.Now.AddHours(1);
cookie.Value = Serialize(model);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
public static Model.UserStat getCurrentUserStat()
{
if (System.Web.HttpContext.Current.Request.Cookies["bloghu"] != null)
return (Model.UserStat)Deserialize(System.Web.HttpContext.Current.Request.Cookies["bloghu"].Value);
else
return new Model.UserStat();
}
static string Serialize(object obj)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
return Convert.ToBase64String(stream.ToArray());
}
static object Deserialize(string str)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
return formatter.Deserialize(stream);
}
本文介绍了一个用于ASP.NET应用程序中管理用户状态的方法。通过使用HttpCookie对象存储序列化的用户状态,并提供了设置和获取用户状态的功能。利用BinaryFormatter进行对象的序列化和反序列化,以便于存储和读取。

被折叠的 条评论
为什么被折叠?



