欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Cookie帮助类

程序员文章站 2022-03-30 21:16:33
using system; using system.collections.generic; using system.linq; using system.web; u...

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.io;
using system.runtime.serialization;
using system.runtime.serialization.formatters.binary;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.data;
using system.configuration;
namespace bll
{
    /// <summary>
    /// cookie操作类
    /// </summary>
    public class cookiehelper
    {
        #region 保存cookie
        /// <summary>
        /// 保存cookie
        /// </summary>
        /// <param name="cookiename">cookie名称</param>
        /// <param name="cookievalue">cookie值</param>
        /// <param name="cookietime">cookie过期时间(小时),0为关闭页面失效</param>
        public static void savecookie(string cookiename, object cookievalue, double cookietime)
        {
            httpcookie mycookie = new httpcookie(cookiename);
            datetime now = datetime.now;
            mycookie.value = convertobjecttostring(cookievalue);
            if (cookietime != 0)
                mycookie.expires = now.addhours(cookietime);
            if (httpcontext.current.response.cookies[cookiename] != null)
                httpcontext.current.response.cookies.remove(cookiename);
            httpcontext.current.response.cookies.add(mycookie);
        }
        private static string convertobjecttostring(object cookievalue)
        {
            binaryformatter bf = new binaryformatter();
            memorystream ms = new memorystream();
            bf.serialize(ms, cookievalue);
            byte[] result = new byte[ms.length];
            result = ms.toarray();
            string temp = system.convert.tobase64string(result);
            ms.flush();
            ms.close();
            return temp;
        }
        #endregion
        #region 取得cookie
        /// <summary>
        /// 取得cookie
        /// </summary>
        /// <param name="cookiename">cookie名称</param>
        /// <returns>cookie的值</returns>
        public static object getcookie(string cookiename)
        {
            httpcookie mycookie = new httpcookie(cookiename);
            mycookie = httpcontext.current.request.cookies[cookiename];
            if (mycookie != null)
                return convertstringtoobject(mycookie.value);
            else
                return null;
        }
        private static object convertstringtoobject(string value)
        {
            byte[] b = system.convert.frombase64string(value);
            memorystream ms = new memorystream(b, 0, b.length);
            binaryformatter bf = new binaryformatter();
            return bf.deserialize(ms);
        }
        #endregion
        #region 清除cookie
        /// <summary>
        /// 清除cookie
        /// </summary>
        /// <param name="cookiename">cookie名称</param>
        public static void clearcookie(string cookiename)
        {
            httpcookie mycookie = new httpcookie(cookiename);
            datetime now = datetime.now;
            mycookie.expires = now.addyears(-2);
            httpcontext.current.response.cookies.add(mycookie);
        }
        #endregion
    }
}

 


作者  wander.chu