.NET常用工具类——COOKIES操作类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ZC.Utils
{
    /// <summary>
    /// <para> </para>
    ///  常用工具类——COOKIES操作类
    /// <para> -------------------------------------------------------------------</para>
    /// <para> WriteCookie:创建COOKIE对象并赋Value值或值集合 [+4重载]</para>
    /// <para> GetCookie:读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回null</para>
    /// <para> DelCookie:删除COOKIE对象</para>
    /// </summary>
    public class CookiesHelper
    {

#region 创建COOKIE对象并赋Value值
        /// <summary>
        /// 创建COOKIE对象并赋Value值
        /// </summary>
        /// <param name="CookiesName">COOKIE对象名</param>
        /// <param name="IExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param>  
        /// <param name="CookiesValue">COOKIE对象Value值</param>
        public static void WriteCookies(string CookiesName, int IExpires, string CookiesValue)
        {
            HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
            objCookie.Value = MD5Helper.DESEncrypt(CookiesValue.Trim());    //加密存储
            if (IExpires > 0)
            {
                if (IExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddMinutes(IExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }

/// <summary>
        /// 创建COOKIE对象并赋Value值
        /// </summary>
        /// <param name="CookiesName">COOKIE对象名</param>
        /// <param name="IExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param>  
        /// <param name="CookiesValue">COOKIE对象Value值</param>
        /// <param name="CookiesDomain">作用域</param>
        public static void WriteCookies(string CookiesName, int IExpires, string CookiesValue, string CookiesDomain)
        {
            HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
            objCookie.Value = MD5Helper.DESEncrypt(CookiesValue.Trim());    //加密存储
            objCookie.Domain = CookiesDomain.Trim();
            if (IExpires > 0)
            {
                if (IExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddMinutes(IExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }

/// <summary>   
        /// 创建COOKIE对象并赋多个KEY键值   
        /// 设键/值如下:   
        /// NameValueCollection myCol = new NameValueCollection();   
        /// myCol.Add("red", "rojo");   
        /// myCol.Add("green", "verde");   
        /// myCol.Add("blue", "azul");   
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge;green:verde;blue:azul”   
        /// </summary>   
        /// <param name="CookiesName">COOKIE对象名</param>   
        /// <param name="IExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param>   
        /// <param name="CookiesKeyValueCollection">键/值对集合</param>
        public static void WriteCookies(string CookiesName, int IExpires, NameValueCollection CookiesKeyValueCollection)
        {
            HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
            foreach (String key in CookiesKeyValueCollection.AllKeys)
            {
                objCookie[key] = MD5Helper.DESEncrypt(CookiesKeyValueCollection[key].Trim());
            }
            if (IExpires > 0)
            {
                if (IExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(IExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }

/// <summary>   
        /// 创建COOKIE对象并赋多个KEY键值   
        /// 设键/值如下:   
        /// NameValueCollection myCol = new NameValueCollection();   
        /// myCol.Add("red", "rojo");   
        /// myCol.Add("green", "verde");   
        /// myCol.Add("blue", "azul");   
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge;green:verde;blue:azul”   
        /// </summary>   
        /// <param name="CookiesName">COOKIE对象名</param>   
        /// <param name="IExpires">COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),</param>   
        /// <param name="CookiesKeyValueCollection">键/值对集合</param>
        /// <param name="CookiesDomain">作用域</param>
        public static void WriteCookies(string CookiesName, int IExpires, NameValueCollection CookiesKeyValueCollection, string CookiesDomain)
        {
            HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
            foreach (String key in CookiesKeyValueCollection.AllKeys)
            {
                objCookie[key] = MD5Helper.DESEncrypt(CookiesKeyValueCollection[key].Trim());
            }
            objCookie.Domain = CookiesDomain.Trim();
            if (IExpires > 0)
            {
                if (IExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(IExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }

#endregion

#region 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回null
        /// <summary>
        /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回null
        /// </summary>
        /// <param name="CookiesName">Cookie对象名称</param>
        /// <returns>返回对象的Value值,返回Value值,如果对象本就不存在,则返回null</returns>
        public static string GetCookies(string CookiesName)
        {
            if (HttpContext.Current.Request.Cookies[CookiesName] == null)
            {
                return null;
            }
            else
            {
                return MD5Helper.DESDecrypt(HttpContext.Current.Request.Cookies[CookiesName].Value);
            }
        }

/// <summary>
        /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回null
        /// </summary>
        /// <param name="CookiesName">Cookie对象名称</param>
        /// <param name="KeyName">键值</param>
        /// <returns>返回对象的Value值,返回Value值,如果对象本就不存在,则返回null</returns>
        public static string GetCookies(string CookiesName, string KeyName)
        {
            if (HttpContext.Current.Request.Cookies[CookiesName] == null)
            {
                return null;
            }
            else
            {
                string strObjValue = MD5Helper.DESDecrypt(HttpContext.Current.Request.Cookies[CookiesName].Value);
                string strKeyName2 = KeyName + "=";
                if (strObjValue.IndexOf(strKeyName2) == -1)
                {
                    return null;
                }
                else
                {
                    return MD5Helper.DESDecrypt(HttpContext.Current.Request.Cookies[CookiesName][KeyName]);
                }
            }
        }
        #endregion

#region 删除COOKIE对象
        /// <summary>
        /// 删除COOKIE对象
        /// </summary>
        /// <param name="CookiesName">Cookie对象名称</param>
        public static void DelCookie(string CookiesName)
        {
            HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
            objCookie.Expires = DateTime.Now.AddYears(-5);
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        #endregion
    }
}

时间: 2024-11-05 19:44:31

.NET常用工具类——COOKIES操作类的相关文章

[C#] 常用工具类——文件操作类

/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在</para> /// <para> IsImgFilename:判断文件名是否为浏览器可以直接显示的图片文件名</para> /// <para> CopyFiles:复制指定目录的所有文件</para> /// <para> MoveFi

C#常用工具类——Excel操作类

/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> /// <para> CreateConnection:根据Excel文件路径和EXCEL驱动版本生成OleConnection对象实例</para> /// <para> ExecuteDataSet:执行一条SQL语句,返回一个DataSet对象</para>

.Net常用工具类——Session操作类

using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts; namespace ZC

C#常用工具类——Excel操作类(ZT)

本文转载于: http://www.cnblogs.com/zfanlong1314/p/3916047.html 1 /// 常用工具类——Excel操作类 2 /// <para> ------------------------------------------------</para> 3 /// <para> CreateConnection:根据Excel文件路径和EXCEL驱动版本生成OleConnection对象实例</para> 4 //

C#语法糖之Cookies操作类 asp.net

用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c" }; //缓存key string key = "cokey"; //获取实例 var cookiesManager = CookiesManager<List<string>>.GetInstance(); //插入缓存 cookiesManager.Add

Cookies操作类

实现代码: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c" }; //缓存key string key = "cokey"; //获取实例 var cookiesManager = CookiesManager<List<string>>.GetInstance(); //插入缓存 cookiesManager.A

Java常用类库之时间操作类——Date、Calendar、DateFormat、SimpleDateFormat及实例操作

学习目标 掌握Date类的使用 可以使用Calendar类取得一个完整的日期 掌握日期格式化的操作 可以使用SimpleDateFormat进行日期的格式化转换操作 编写取得日期的操作类 进一步掌握Calendar类的使用 进一步掌握SimpleDateFormat类的使用 Date类是一个较为常用的类,但是其操作的日期格式会有一些不符合个人要求,而如果想要进一步取得一些自己需要的时间,则可以使用Calendar类. Date类 在java.util包中定义了Date类,Date类本身使用非常简

JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/

/** * Math类.Random类.Arrays类:具体查JAVA手册...... */ public class Main { public static void main(String[] args) { String[] s1 = {"a","b","c","d","e"}; String[] s2 = {"a","b","c",&qu

0913 完整修改,删除-实体类-数据操作类

<完整修改删除> 昨天我们使用c#访问数据库会有很多弊端,如果数据库中没有这一条信息也会返回删除成功 今天完整修改删除就会克服这个困难 第一步 需要先查询 #region 查询                Console.WriteLine("学号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "    " + "