[C#]Cookie中保存Object类型数据[转]

对于Web中Object类型数据的保存,一般都是用Session,ViewState.不过这样一来,用户退出或者关闭IE后,就无法保存了,没办法,只能用Cookie试试了

原理:利用序列化/反序列化,保存Object类型的时候,序列化为字符串保存,取用的时候,再反序列化为对象。

Cookie管理核心代码:

 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
namespace  CommonLibrary.CookieManager
{
    
public   class  ObjectListCookie
    {
        
public  ObjectListCookie()
        {
            
//
        }
        
///   <summary>
        
///  将Object类型添加到Cookie项的子项中
        
///   </summary>
        
///   <param name="CookieName"> 保存Cookie项名字 </param>
        
///   <param name="CookieItem"> 要添加到子项的对象 </param>
        
///   <param name="MaxCount"> 最大项数,0代表不限 </param>
        
public   void  AddCookieItem( string  CookieName, Object CookieItem,  int  MaxCount)
        {
           HttpCookie cookie;
            
if  (HttpContext.Current.Request.Cookies[CookieName]  !=   null )
            {
                cookie 
=  HttpContext.Current.Request.Cookies[CookieName];
            }
            
else
            {
                cookie 
=   new  HttpCookie(CookieName);
            }

            
// 保证不超过最大项数

            
if  (cookie.Values.Count  >=  MaxCount)
            {
                
int  DelCount  =  cookie.Values.Count  -  MaxCount  +   1 ;
                
for  ( int  i  =   0 ; i  <  DelCount; i ++ )
                {
                    cookie.Values.Remove(cookie.Values.GetKey(
0 ));
                }
            }
            
string  cookieValue  =  ObjectStrConvert.ObjectToBase64Str(CookieItem);
            cookie.Values.Add(System.DateTime.Now.ToString(
" yyyyMMddHHmmssfff " ), cookieValue);
            cookie.Expires 
=  DateTime.MaxValue;    
            
if  (HttpContext.Current.Request.Cookies[CookieName]  !=   null )
            {
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
           
else
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
           }
        }

        
///   <summary>
        
///  得到指定Cookie项中保存的对象列表
        
///   </summary>
        
///   <param name="CookieName"> Cookie项名字 </param>
        
///   <param name="ItemObjType"> 子项对象类型 </param>
        
///   <returns></returns>
        
public  List < Object >  GetObjectListFromCookie( string  CookieName, Type ItemObjType)
        {
            HttpCookie cookie;
            List
< Object >  list  =   new  List < Object > ();
            
if  (HttpContext.Current.Request.Cookies[CookieName]  !=   null )
            {
                cookie 
=  HttpContext.Current.Request.Cookies[CookieName];
                
for  ( int  i  =   0 ; i  <  cookie.Values.Count; i ++ )
                {
                    Object obj 
=  ObjectStrConvert.Base64StrToObject(cookie.Values[i], ItemObjType);
                    list.Add(obj);
                }
            }        

            
return  list;
        }

        
///   <summary>
        
///  清除指定Cookie的所有子项
        
///   </summary>
        
///   <param name="CookieName"> Cookie名字 </param>

        
public   void  ClearCookieItems( string  CookieName)
        {
            
if  (HttpContext.Current.Request.Cookies[CookieName]  !=   null )
            {
                HttpCookie cookie 
=  HttpContext.Current.Request.Cookies[CookieName];
                cookie.Values.Clear();
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/zhaoming/archive/2009/06/13/1502454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值