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); } }
原文地址:https://www.cnblogs.com/jiangqw/p/12121249.html
时间: 2024-10-09 13:27:52