asp.net Cookie操作类
2010-07-26 17:49
92 查看
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; namespace Xyz.Common { /// <summary> ///cook 的摘要说明 /// </summary> public class cooks { /// <summary> /// 保存一个Cookie /// </summary> /// <param name="CookieName">Cookie名称</param> /// <param name="CookieValue">Cookie值</param> /// <param name="CookieTime">Cookie过期时间(小时),0为关闭页面失效</param> public void SaveCookie(string CookieName, string CookieValue, double CookieTime) { HttpCookie myCookie = new HttpCookie(CookieName); DateTime now = DateTime.Now; myCookie.Value = CookieValue; if (CookieTime != 0) { myCookie.Expires = now.AddDays(CookieTime); if (HttpContext.Current.Response.Cookies[CookieName] != null) HttpContext.Current.Response.Cookies.Remove(CookieName); HttpContext.Current.Response.Cookies.Add(myCookie); } else { if (HttpContext.Current.Response.Cookies[CookieName] != null) HttpContext.Current.Response.Cookies.Remove(CookieName); HttpContext.Current.Response.Cookies.Add(myCookie); } } /// <summary> /// 取得CookieValue /// </summary> /// <param name="CookieName">Cookie名称</param> /// <returns>Cookie的值</returns> public string GetCookie(string CookieName) { HttpCookie myCookie = new HttpCookie(CookieName); myCookie = HttpContext.Current.Request.Cookies[CookieName]; if (myCookie != null) return myCookie.Value; else return null; } /// <summary> /// 清除CookieValue /// </summary> /// <param name="CookieName">Cookie名称</param> public void ClearCookie(string CookieName) { HttpCookie myCookie = new HttpCookie(CookieName); DateTime now = DateTime.Now; myCookie.Expires = now.AddYears(-2); HttpContext.Current.Response.Cookies.Add(myCookie); } } }
相关文章推荐
- ASP.NET-------Cookie静态操作类
- asp.net Cookie操作类
- 分享一个asp.net Cookie操作类
- ASP.NET中Cookie编程简明参考
- [导入]关于asp.net c#中对cookie的操作
- ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等) .
- ASP.NET之Cookie(坑爹的Response.Cookies.Remove)
- ASP.NET Cookie 概述【转】
- ASP.NET 2.0中预设的cookie
- 在无cookie模式中,会话话状态的ID将会自动保存在ASP.NET的查询字符串中
- ASP.NET基础培训 - Cookie的正确利用
- IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题
- ASP.Net的Cookie实现
- ASP.NET之Cookie(坑爹的Response.Cookies.Remove)
- asp.net 4.0 遨游3 cookie问题一例
- Asp.net 基于Cookie简易的权限判断
- ASP.NET Application,Session,Cookie和ViewState等对象用法和区别
- ASP.NET中的cookie的应用,对应多个值的情
- Asp.net中Cookie简明参考