.net Cookie的操作
2018-06-22 07:49:37来源:未知 阅读 ()
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); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:IIS与asp.net管道
- asp.net源程序编译为dll文件并调用的实现过程 2020-03-29
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据 2020-03-29
- Asp.Net中WebForm的生命周期 2020-03-29
- ASP.NET使用Ajax返回Json对象的方法 2020-03-23
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