当前位置: 移动技术网 > IT编程>开发语言>.net > Cookie帮助类

Cookie帮助类

2019年04月19日  | 移动技术网IT编程  | 我要评论

像中枪一样 歌词,小鬼也穿越,武神下载

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网