C# (Cookie)基本操作
2018-06-17 21:50:26来源:未知 阅读 ()
在Common中新建一个CookieHelper,全局调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Common { public class CookieHelper { /// <summary> /// 设置cookie /// </summary> /// <param name="cookieName">cookie名称</param> /// <param name="cookieValue">cookie值</param> /// <param name="domain">作用域,为空就不写入作用域</param> public static void SetCookie(String cookieName, String cookieValue, string domain) { if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return; if (HttpContext.Current != null) { HttpCookie cookie = new HttpCookie(cookieName, cookieValue); if (domain.Length > 0) { cookie.Domain = domain; } cookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 设置cookie /// </summary> /// <param name="cookieName">cookie名称</param> /// <param name="cookieValue">cookie值</param> /// <param name="domain">作用域,为空就不写入作用域</param> /// <param name="day">有效时间</param> public static void SetCookie(String cookieName, String cookieValue, string domain, int day) { if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return; if (HttpContext.Current != null) { HttpCookie cookie = new HttpCookie(cookieName, cookieValue); if (domain.Length > 0) { cookie.Domain = domain; } cookie.HttpOnly = true; cookie.Expires = DateTime.Now.AddDays(day); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 设置cookie过期 /// </summary> /// <param name="cookieName">需要过期的cookie名称</param> public static void ExpireCookie(String cookieName) { if (String.IsNullOrEmpty(cookieName)) return; if (HttpContext.Current != null) { HttpCookie cookie = new HttpCookie(cookieName, string.Empty); cookie.HttpOnly = true; cookie.Expires = DateTime.Now.AddYears(-5); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 获取对应Cookie名称的值 /// </summary> /// <param name="cookieName">Cookie 的名称</param> /// <returns></returns> public static string GetCookie(string cookieName) { if (string.IsNullOrEmpty(cookieName)) return string.Empty; if (System.Web.HttpContext.Current == null) return string.Empty; if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return string.Empty; else return System.Web.HttpContext.Current.Request.Cookies[cookieName].Value; } /// <summary> /// 判断对应的Cookie是否存在 /// </summary> /// <param name="cookieName">Cookie 的名称</param> /// <returns></returns> public static bool ExistCookie(string cookieName) { if (string.IsNullOrEmpty(cookieName) || System.Web.HttpContext.Current == null) return false; if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return false; if (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value == null) return false; return (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value.Length > 0); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:C# 中Switch case 返回不止用break
下一篇:ajax基础一
- RabbitMQ原理、集群、基本操作及常见故障处理 2019-08-23
- php cookie工作原理与实例详解 2019-08-09
- python数据库-MySQL单表查询基本操作(50) 2019-07-24
- python数据库-MySQL数据库的增删改查基本操作(49) 2019-07-24
- 02、MySQL—数据库基本操作 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash