博客
关于我
common -- CookiesHelper
阅读量:295 次
发布时间:2019-03-01

本文共 6509 字,大约阅读时间需要 21 分钟。

/*----------------------------------------------------------------// Copyright (C) 2010 *****
// 版权所有。 //// 文件名:CookiesHelper.cs// 文件功能描述:Cookies处理类//// // 创建标识:******//// 修改标识:// 修改描述://// 修改标识:// 修改描述://----------------------------------------------------------------*/using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace *****TOOLS{    public class CookiesHelper    {        #region 获取Cookie        ///         /// 获得Cookie的值        ///         ///         /// 
public static string GetCookieValue(string cookieName) { return GetCookieValue(cookieName, null); } /// /// 获得Cookie的值 /// /// /// ///
public static string GetCookieValue(string cookieName, string key) { HttpRequest request = HttpContext.Current.Request; if (request != null) return GetCookieValue(request.Cookies[cookieName], key); return ""; } /// /// 获得Cookie的子键值 /// /// /// ///
public static string GetCookieValue(HttpCookie cookie, string key) { if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) return cookie.Values[key]; else return cookie.Value; } return ""; } /// /// 获得Cookie /// /// ///
public static HttpCookie GetCookie(string cookieName) { HttpRequest request = HttpContext.Current.Request; if (request != null) return request.Cookies[cookieName]; return null; } #endregion #region 删除Cookie /// /// 删除Cookie /// /// public static void RemoveCookie(string cookieName) { RemoveCookie(cookieName, null); } /// /// 删除Cookie的子键 /// /// /// public static void RemoveCookie(string cookieName, string key) { HttpResponse response = HttpContext.Current.Response; if (response != null) { HttpCookie cookie = response.Cookies[cookieName]; if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) cookie.Values.Remove(key); else response.Cookies.Remove(cookieName); } } } #endregion #region 设置/修改Cookie /// /// 设置Cookie子键的值 /// /// /// /// public static void SetCookie(string cookieName, string key, string value) { SetCookie(cookieName, key, value, null); } /// /// 设置Cookie值 /// /// /// public static void SetCookie(string key, string value) { SetCookie(key, null, value, null); } /// /// 设置Cookie值和过期时间 /// /// /// /// public static void SetCookie(string key, string value, DateTime expires) { SetCookie(key, null, value, expires); } /// /// 设置Cookie过期时间 /// /// /// public static void SetCookie(string cookieName, DateTime expires) { SetCookie(cookieName, null, null, expires); } /// /// 设置Cookie /// /// /// /// /// public static void SetCookie(string cookieName, string key, string value, DateTime? expires) { HttpResponse response = HttpContext.Current.Response; if (response != null) { HttpCookie cookie = response.Cookies[cookieName]; if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) cookie.Values.Set(key, value); else if (!string.IsNullOrEmpty(value)) cookie.Value = value; if (expires != null) cookie.Expires = expires.Value; response.SetCookie(cookie); } } } #endregion #region 添加Cookie /// /// 添加Cookie /// /// /// public static void AddCookie(string key, string value) { AddCookie(new HttpCookie(key, value)); } /// /// 添加Cookie /// /// /// /// public static void AddCookie(string key, string value, DateTime expires) { HttpCookie cookie = new HttpCookie(key, value); cookie.Expires = expires; AddCookie(cookie); } /// /// 添加为Cookie.Values集合 /// /// /// /// public static void AddCookie(string cookieName, string key, string value) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Values.Add(key, value); AddCookie(cookie); } /// /// 添加为Cookie集合 /// /// Cookie名称 /// 过期时间 public static void AddCookie(string cookieName, DateTime expires) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Expires = expires; AddCookie(cookie); } /// /// 添加为Cookie.Values集合 /// /// /// /// /// public static void AddCookie(string cookieName, string key, string value, DateTime expires) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Expires = expires; cookie.Values.Add(key, value); AddCookie(cookie); } /// /// 添加Cookie /// /// public static void AddCookie(HttpCookie cookie) { HttpResponse response = HttpContext.Current.Response; if (response != null) { //指定客户端脚本是否可以访问[默认为false] cookie.HttpOnly = true; //指定统一的Path,比便能通存通取 cookie.Path = "/"; //设置跨域,这样在其它二级域名下就都可以访问到了 //cookie.Domain = "chinesecoo.com"; response.AppendCookie(cookie); } } #endregion }}

 

转载地址:http://pzko.baihongyu.com/

你可能感兴趣的文章
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>