.net Cookie的操作

using System;
using System.Collections.Generic;
using System.Web;

namespace Zhong.Core
{
    /// <summary>
    /// Cookie操作类
    /// </summary>
    public class CookieHelper
    {
        private static readonly string CookieName = "Zhong";
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="values">键/值对</param>
        /// <param name="expires">过期超时时间(秒),为0时不设置过期时间</param>
        /// <param name="domain">域名</param>
        /// <param name="path">路径</param>
        public static void SetCookie(string name, Dictionary<string, string> values, int expires, string domain = null, string path = null)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            foreach (KeyValuePair<string, string> kv in values)
            {
                cookie.Values.Add(kv.Key, kv.Value);
            }
            if (domain != null)
            {
                cookie.Domain = domain;
            }
            if (path != null)
            {
                cookie.Path = path;
            }
            if (expires != 0)
            {
                cookie.Expires = DateTime.Now.AddSeconds(expires);  //过期时间
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetCookie(string key, string value)
        {
            SetCookie(CookieName, new Dictionary<string, string> { { key, value } }, 0);
        }
        /// <summary>
        /// 根据名称与键读取Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static string GetCookie(string name, string key)
        {
            string returnVal = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie != null)
            {
                returnVal = cookie[key];
            }
            return returnVal;
        }
        /// <summary>
        /// 根据键读取Cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            return GetCookie(CookieName, key);
        }
        /// <summary>
        /// 根据名称获取Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static string GetCookieByName(string name)
        {
            string returnVal = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie!= null)
            {
                returnVal = cookie.Value;
            }
            return returnVal;
        }
        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="name">名称</param>
        public static void DeleteCookie(string name)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddYears(-1);
                cookie.Values.Clear();
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
时间: 2024-08-03 09:41:30

.net Cookie的操作的相关文章

Jquery对Cookie的操作

第一步:先引用jQuery的插件jquery-1.9.1.min.js 第二步:引用jquery.cookie.js插件 下对cookie的操作: $.cookie("cookieName", "cookieValue", { expires: 10, //有效日期 以 天 为单位 如果此项未设置则 浏览器关闭时就失效 path: "/" //cookie的路径 //,secure:true //默认值:false.如果为true,cookie的

cookie的操作

使用cookie cookie的使用 1. 设置cookie 格式:名字=值 不会覆盖 过期时间:expires=时间 日期对象的使用 封装函数 2.读取cookie 字符串分割 3.删除cookie 已经过期:设置cookie过期,即是删除cookie cookie的domain和path 1.path:一般不是只有设置cookie的页面才可以读取cookie,path可以改变可读取到cookie的路径,要是整个网站都可以访问,即根目录及根目录下都可以访问到,即:"path=/'' 2.dom

java对cookie的操作

java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题. 建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 1 2 3 4 HttpServletRequest request  HttpServletResponse response Cookie cookie = new Cookie("cookiename","cookievalue");

jquery.cookie.js 操作cookie实现记住密码功能的实现代码

jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下. 复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready(function() { if ($.cookie("rmbUser") == "true") { $("#rmbUser").attr("checked", true); $("#user").val($.coo

yii cookie ,session 操作

一,在Yii中使用session 1,CHttpSession 与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $value; 在yii中,session已经被封装. To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy().

JS cookie 读写操作

/*** ** 功能: cookie操作对象 ***/ var cookies = { /*** ** 功能: 写入cookie操作 ** 参数: name cookie名称 ** value cookie值 ** expires 过期时间 ** path 路径 ** domain 域 ***/ set: function (name, value, expires, path, domain) { expires = new Date(new Date().getTime() + (((typ

asp.net对cookie的操作

创建cookie: 1 HttpCookie cookie = new HttpCookie("CurrentUser"); //创建一个名称为CurrentUser 的cookie对象 2 cookie.Values.Add("UserId", "1"); //在cookie对象添加 一项键值对 3 cookie.Values.Add("UserName", "用户名称"); //在cookie对象添加

C# Winform WebBrowser中对控件和cookie的操作

来看一下代码吧 webBrowser1.Document.GetElementById("J_Quick2Static").InvokeMember("click"); webBrowser1.Document.GetElementById("TPL_username_1").SetAttribute("value", "13025143230"); webBrowser1.Document.GetElem

php中cookie的操作

php设置和获取cookie 创建cookie setcookie('mycookie','value',time()+86400);//参数3 cookie过期时间//函数原型:int setcookie(string name,string value,int expire,string path,string domain,int secure) echo($mycookie);echo($HTTP_COOKIE_VARS['mycookie']);echo($_COOKIE['mycoo