【.NET】Cookie操作类
2015-11-27 15:28
405 查看
public static class CookiesHelper { /// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键</param> /// <param name="strValue">键值</param> /// <param name="strDay">有效天数</param> /// <returns></returns> public static bool SetCookie(string strName, string strValue, int strDay) { try { HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName]; if (Cookie == null) { Cookie = new HttpCookie(strName); } Cookie.Expires = DateTime.Now.AddDays(strDay); Cookie.Value = strValue; System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); return true; } catch { return false; } } /// <summary> /// 读取Cookies /// </summary> /// <param name="strName">主键</param> /// <returns></returns> public static string GetCookie(string strName) { HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName]; if (Cookie != null) { return Cookie.Value.ToString(); } else { return null; } } /// <summary> /// 删除Cookies /// </summary> /// <param name="strName">主键</param> /// <returns></returns> public static bool DelCookie(string strName) { try { HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];// new HttpCookie(strName); Cookie.Expires = DateTime.Now.AddDays(-1); System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); return true; } catch { return false; } } }
相关文章推荐
- redis 本机链接服务端命令
- 报表设计中参数、脚本的使用以及生成系统时间
- 机器学习实战k-近邻算法(kNN)应用之改进婚恋网站配对效果代码解
- 抽象类和接口的应用
- eclipse导入工程失败解决方法
- 第一篇博客
- 关于安装Zookeeper 服务端问题解决
- 自定义UIbutton的样式
- 红黑树的C++实现(2)
- thinkphp中url的生成U()方法
- Android中的MVP架构
- linux shell “(())” 双括号运算符使用
- js获取子元素
- mysql的一些问题
- 安卓中轻量级数据库SQLite的使用
- spring hadoop 系列(二)
- word如何关联博客
- javaWeb 复习系列(二)
- 报表设计中参数、脚本的使用以及生成系统时间
- Response.Flush和Response.BufferOutput