Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET技巧

asp.net Cookie操作类

来源:中文源码网    浏览:352 次    日期:2024-04-21 08:23:23
【下载文档:  asp.net Cookie操作类.txt 】


asp.net Cookie操作类
复制代码 代码如下:using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Data; using System.Configuration; namespace Jhgl.Smart { /// /// Cookie操作类 /// public class Cookie { /// /// 保存一个Cookie /// /// Cookie名称 /// Cookie值 /// Cookie过期时间(小时),0为关闭页面失效 public static void SaveCookie(string CookieName, string CookieValue, double CookieTime) { HttpCookie myCookie = new HttpCookie(CookieName); DateTime now = DateTime.Now; myCookie.Value = CookieValue; if (CookieTime != 0) { //有两种方法,第一方法设置Cookie时间的话,关闭浏览器不会自动清除Cookie //第二方法不设置Cookie时间的话,关闭浏览器会自动清除Cookie ,但是有效期 //多久还未得到证实。 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); } } /// /// 取得CookieValue /// /// Cookie名称 /// Cookie的值 public static string GetCookie(string CookieName) { HttpCookie myCookie = new HttpCookie(CookieName); myCookie = HttpContext.Current.Request.Cookies[CookieName]; if (myCookie != null) return myCookie.Value; else return null; } /// /// 清除CookieValue /// /// Cookie名称 public static void ClearCookie(string CookieName) { HttpCookie myCookie = new HttpCookie(CookieName); DateTime now = DateTime.Now; myCookie.Expires = now.AddYears(-2); HttpContext.Current.Response.Cookies.Add(myCookie); } } }

相关内容