呼之欲出 WebMail 开发手记 (三) 用户信息存取

关于用户登录后的信息存储方式的讨论,前有古人,后也会有来者。(我就不捣乱了~~)

 

一般有以下三种方式做为选择:

一、存储到 Session 中;
二、存储到 Cookie 中;
三、存储到 数据库 中。

 

本系统在每个用户登录系统后,先将用户信息序列化,然后再存储到 Cookie 中。

 

附代码:

 


using System;

/***************************************
 ********  里奥特在线邮件收发系统  *****
 **************************************
*/

namespace Lyout.WebMail {
    
/// 
    
/// 用户信息
    
/// 

    [Serializable]
    
public class UserInfo {
        
private DateTime loginDate;
        
private int userID;
        
private string username;
        
private string nickname;
        
private int roleID = 0;

        
public UserInfo() {
        }


        
public UserInfo(int userID) {
            
this.userID = userID;
            
this.username = string.Empty;
            
this.nickname = string.Empty;
            
this.loginDate = DateTime.Now;
        }


        
public UserInfo(int userID, string username) {
            
this.userID = userID;
            
this.username = username;
            
this.nickname = username;
            
this.loginDate = DateTime.Now;
        }


        
public UserInfo(int userID, string username, string nickname) : this(userID, username) {
            
this.nickname = nickname;
        }


        
public UserInfo(int userID, string username, DateTime loginDate) {
            
this.userID = userID;
            
this.username = username;
            
this.nickname = username;
            
this.loginDate = loginDate;
        }


        
public UserInfo(int userID, string username, string nickname, DateTime loginDate) : this(userID, username, loginDate) {
            
this.nickname = nickname;
        }

        
/// 
        
/// 登录日期
        
/// 

        public DateTime LoginDate {
            
get {
                
return this.loginDate;
            }

            
set {
                
this.loginDate = value;
            }

        }

        
/// 
        
/// 用户ID
        
/// 

        public int UserID {
            
get {
                
return this.userID;
            }

            
set {
                
this.userID = value;
            }

        }

        
/// 
        
/// 登录名
        
/// 

        public string UserName {
            
get {
                
return this.username;
            }

            
set {
                
this.username = value;
            }

        }

        
/// 
        
/// 呢称
        
/// 

        public string NickName {
            
get {
                
return this.nickname;
            }

            
set {
                
this.nickname = value;
            }

        }

        
/// 
        
/// 角色ID
        
/// 

        public int RoleID {
            
get {
                
return roleID;
            }

            
set {
                roleID 
= value;
            }

        }

    }

}


 

 


using System;
using System.Web.SessionState;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

/***************************************
 ********  里奥特在线邮件收发系统  *****
 **************************************
*/

namespace Lyout.WebMail {
    
/// 
    
/// 用户信息操作
    
/// 

    public class UserHelper {
        
private static readonly string UserIDKey = "UCO_USERIDKEY";

        
/// 
        
/// 删除
        
/// 

        
/// 

        public static void Delete(HttpCookieCollection cookies) {
            cookies.Remove(UserIDKey);
        }


        
/// 
        
/// 从缓存中取出用户数据
        
/// 

        
/// 
        
/// 

        public static UserInfo GetUserInfo(HttpCookieCollection cookies) {
            
if (cookies[UserIDKey] != null{
                
string cookiedata = cookies[UserIDKey].Value;
                
if (!string.IsNullOrEmpty(cookiedata)) {
                    
// 反序列化用户信息
                    string userData = HttpContext.Current.Server.UrlDecode(cookiedata);
                    
byte[] bt = Convert.FromBase64String(userData);
                    
using (Stream smNew = new MemoryStream(bt)) {
                        IFormatter fmNew 
= new BinaryFormatter();
                        
return (UserInfo)fmNew.Deserialize(smNew);
                    }

                }

            }

            
return null;
        }


        
/// 
        
/// 把用户信息存储到缓存中
        
/// 

        
/// 
        
/// 

        public static void StoreUserInfo(HttpCookieCollection cookies, UserInfo info) {
            
if (cookies!=null{
                IFormatter fm 
= new BinaryFormatter();
                MemoryStream sm 
= new MemoryStream();

                
// 序列化用户信息
                fm.Serialize(sm, info);
                sm.Seek(
0, SeekOrigin.Begin);

                
// 转为 base64 格式
                byte[] byt = new byte[sm.Length];
                byt 
= sm.ToArray();
                
string userData = Convert.ToBase64String(byt);
                sm.Flush();

                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); 
            }

        }


        
public static void StoreUserInfo(HttpCookieCollection cookies, int userID) {
            StoreUserInfo(cookies, 
new UserInfo(userID));
        }


        
public static void StoreUserInfo(HttpCookieCollection cookies, int userID, string username) {
            StoreUserInfo(cookies, 
new UserInfo(userID, username));
        }


        
public static void StoreUserInfo(HttpCookieCollection cookies, int userID, string username, DateTime loginDate) {
            StoreUserInfo(cookies, 
new UserInfo(userID, username, loginDate));
        }

    }

}


 

以下是关于序列化的:

序列化定义

    序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。


序列化的目的

  1. 以某种存储形式使自定义对象持久化
  2. 将对象从一个地方传递到另一个地方。
  3. 对象封送,远程服务甚至网络数据流都运用了序列化的技术。

 

请使用浏览器的分享功能分享到微信等