代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
CookiesHelper.cs
作者:
dezai
/ 发布于
2014/3/25
/
480
using System; using System.Collections.Generic; using System.Text; using System.Web; namespace TLibrary.ObjectHelper { public class CookiesHelper { #region 获取Cookie /// <summary> /// 获得Cookie的值 /// </summary> /// <param name="cookieName"></param> /// <returns></returns> public static string GetCookieValue(string cookieName) { return GetCookieValue(cookieName, null); } /// <summary> /// 获得Cookie的值 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <returns></returns> public static string GetCookieValue(string cookieName,string key) { HttpRequest request = HttpContext.Current.Request; if (request != null) return GetCookieValue(request.Cookies[cookieName], key); return ""; } /// <summary> /// 获得Cookie的子键值 /// </summary> /// <param name="cookie"></param> /// <param name="key"></param> /// <returns></returns> 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 ""; } /// <summary> /// 获得Cookie /// </summary> /// <param name="cookieName"></param> /// <returns></returns> public static HttpCookie GetCookie(string cookieName) { HttpRequest request = HttpContext.Current.Request; if (request != null) return request.Cookies[cookieName]; return null; } #endregion #region 删除Cookie /// <summary> /// 删除Cookie /// </summary> /// <param name="cookieName"></param> public static void RemoveCookie(string cookieName) { RemoveCookie(cookieName,null); } /// <summary> /// 删除Cookie的子键 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> 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 /// <summary> /// 设置Cookie子键的值 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> public static void SetCookie(string cookieName, string key, string value) { SetCookie(cookieName, key, value, null); } /// <summary> /// 设置Cookie值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetCookie(string key, string value) { SetCookie(key, null, value, null); } /// <summary> /// 设置Cookie值和过期时间 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> public static void SetCookie(string key, string value, DateTime expires) { SetCookie(key, null, value, expires); } /// <summary> /// 设置Cookie过期时间 /// </summary> /// <param name="cookieName"></param> /// <param name="expires"></param> public static void SetCookie(string cookieName, DateTime expires) { SetCookie(cookieName, null, null, expires); } /// <summary> /// 设置Cookie /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> 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 /// <summary> /// 添加Cookie /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddCookie(string key, string value) { AddCookie(new HttpCookie(key, value)); } /// <summary> /// 添加Cookie /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> public static void AddCookie(string key, string value, DateTime expires) { HttpCookie cookie = new HttpCookie(key, value); cookie.Expires = expires; AddCookie(cookie); } /// <summary> /// 添加为Cookie.Values集合 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> public static void AddCookie(string cookieName,string key, string value) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Values.Add(key, value); AddCookie(cookie); } /// <summary> /// 添加为Cookie集合 /// </summary> /// <param name="cookieName">Cookie名称</param> /// <param name="expires">过期时间</param> public static void AddCookie(string cookieName, DateTime expires) { HttpCookie cookie = new HttpCookie(cookieName); cookie.Expires = expires; AddCookie(cookie); } /// <summary> /// 添加为Cookie.Values集合 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> 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); } /// <summary> /// 添加Cookie /// </summary> /// <param name="cookie"></param> 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 } }
试试其它关键字
CookiesHelper
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3