.net Cookie操作公共方法 CookieHelper
Cookie操作类
/// <summary> /// Cookie帮助类 /// </summary> public class CookieHelper { /// <summary> /// 写cookie值 /// </summary> /// <param name="cookieName">名称</param> /// <param name="cookieValue">值</param> public static void AddCookie(string cookieName, string cookieValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie == null) { cookie = new HttpCookie(cookieName); } cookie.Value = cookieValue; HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 写cookie值 /// </summary> /// <param name="cookieName">名称</param> /// <param name="cookieValue">值</param> /// <param name="expires">过期时间(分钟)</param> public static void AddCookie(string cookieName, string cookieValue, int expires) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie == null) { cookie = new HttpCookie(cookieName); } cookie.Value = cookieValue; cookie.Expires = DateTime.Now.AddMinutes(expires); HttpContext.Current.Response.AppendCookie(cookie); } /// <summary> /// 读cookie值 /// </summary> /// <param name="cookieName">名称</param> /// <returns>cookie值</returns> public static string GetCookie(string cookieName) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null) { return HttpContext.Current.Request.Cookies[cookieName].Value.ToString(); } return ""; } /// <summary> /// 删除Cookie对象 /// </summary> /// <param name="cookieName">Cookie对象名称</param> public static void RemoveCookie(string cookieName) { HttpCookie objCookie = new HttpCookie(cookieName.Trim()); objCookie.Expires = DateTime.Now.AddYears(-5); HttpContext.Current.Response.Cookies.Add(objCookie); } }
作者:apegu
地址:http://www.cnblogs.com/jiangqw
如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮。