用户信息存储一般有以下三种方式做为选择:
一、存储到 Session 中;
二、存储到 Cookie 中;
三、存储到 数据库 中。
本系统在每个用户登录系统后,先将用户信息序列化,然后再存储到 Cookie 中。
(附代码)
关于用户登录后的信息存储方式的讨论,前有古人,后也会有来者。(我就不捣乱了~~)
一般有以下三种方式做为选择:
一、存储到 Session 中;
二、存储到 Cookie 中;
三、存储到 数据库 中。
本系统在每个用户登录系统后,先将用户信息序列化,然后再存储到 Cookie 中。
附代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
用户信息类 UserInfo
using System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************************************
******** 里奥特在线邮件收发系统 *****
***************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace Lyout.WebMail
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用户信息
/// </summary>
[Serializable]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class UserInfo
{
private DateTime loginDate;
private int userID;
private string username;
private string nickname;
private int roleID = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo(int userID)
{
this.userID = userID;
this.username = string.Empty;
this.nickname = string.Empty;
this.loginDate = DateTime.Now;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo(int userID, string username)
{
this.userID = userID;
this.username = username;
this.nickname = username;
this.loginDate = DateTime.Now;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo(int userID, string username, string nickname) : this(userID, username)
{
this.nickname = nickname;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo(int userID, string username, DateTime loginDate)
{
this.userID = userID;
this.username = username;
this.nickname = username;
this.loginDate = loginDate;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserInfo(int userID, string username, string nickname, DateTime loginDate) : this(userID, username, loginDate)
{
this.nickname = nickname;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登录日期
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime LoginDate
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.loginDate;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.loginDate = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用户ID
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int UserID
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.userID;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.userID = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登录名
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string UserName
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.username;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.username = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 呢称
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string NickName
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return this.nickname;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
this.nickname = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 角色ID
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int RoleID
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return roleID;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
roleID = value;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
用户信息操作类 UserHelper
using System;
using System.Web.SessionState;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************************************
******** 里奥特在线邮件收发系统 *****
***************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace Lyout.WebMail
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用户信息操作
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class UserHelper
{
private static readonly string UserIDKey = "UCO_USERIDKEY";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除
/// </summary>
/// <param name="cookies"></param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void Delete(HttpCookieCollection cookies)
{
cookies.Remove(UserIDKey);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从缓存中取出用户数据
/// </summary>
/// <param name="cookies"></param>
/// <returns></returns>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static UserInfo GetUserInfo(HttpCookieCollection cookies)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (cookies[UserIDKey] != null)
{
string cookiedata = cookies[UserIDKey].Value;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!string.IsNullOrEmpty(cookiedata))
{
// 反序列化用户信息
string userData = HttpContext.Current.Server.UrlDecode(cookiedata);
byte[] bt = Convert.FromBase64String(userData);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using (Stream smNew = new MemoryStream(bt))
{
IFormatter fmNew = new BinaryFormatter();
return (UserInfo)fmNew.Deserialize(smNew);
}
}
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 把用户信息存储到缓存中
/// </summary>
/// <param name="cookies"></param>
/// <param name="info"></param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void StoreUserInfo(HttpCookieCollection cookies, UserInfo info)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (cookies!=null)
{
IFormatter fm = new BinaryFormatter();
MemoryStream sm = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化用户信息
fm.Serialize(sm, info);
sm.Seek(0, SeekOrigin.Begin);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 转为 base64 格式
byte[] byt = new byte[sm.Length];
byt = sm.ToArray();
string userData = Convert.ToBase64String(byt);
sm.Flush();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cookies.Remove(UserIDKey);
// 存储到 Cookie 中
HttpCookie cookie = new HttpCookie(UserIDKey);
cookie.Value = HttpContext.Current.Server.UrlEncode(userData);
// 有效期一天
cookie.Expires = DateTime.Now.AddDays(1);
cookies.Add(cookie);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void StoreUserInfo(HttpCookieCollection cookies, int userID)
{
StoreUserInfo(cookies, new UserInfo(userID));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void StoreUserInfo(HttpCookieCollection cookies, int userID, string username)
{
StoreUserInfo(cookies, new UserInfo(userID, username));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void StoreUserInfo(HttpCookieCollection cookies, int userID, string username, DateTime loginDate)
{
StoreUserInfo(cookies, new UserInfo(userID, username, loginDate));
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
以下是关于序列化的:
序列化定义
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。
序列化的目的
- 以某种存储形式使自定义对象持久化;
- 将对象从一个地方传递到另一个地方。
- 对象封送,远程服务甚至网络数据流都运用了序列化的技术。