首页 > 代码库 > CookieHelper
CookieHelper
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace Common{ public class CookieHelper { /// <summary> /// 保存一个Cookie /// </summary> /// <param name="CookieName">Cookie名称</param> /// <param name="CookieValue">Cookie值</param> /// <param name="CookieTime">Cookie过期时间(小时),0为关闭页面失效</param> 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) { 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 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; } /// <summary> /// 清除CookieValue /// </summary> /// <param name="CookieName">Cookie名称</param> 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); } #region 清除Cookie /// <summary> /// 清除Cookie /// </summary> /// <param name="cookieName"></param> public static void ClearCookie(string cookieName, string domain, string path) { cookieName = Encode(cookieName); HttpCookie cookie = new HttpCookie(cookieName); cookie.Domain = domain; cookie.Path = path; cookie.Expires = DateTime.Now.AddDays(-360); System.Web.HttpContext.Current.Response.Cookies.Add(cookie); } /// <summary> /// 从多值Cookie移除个subKey /// </summary> /// <param name="cookieName"></param> /// <param name="subKey"></param> public static void ClearCookie(string cookieName, string subKey, string domain, string path) { cookieName = Encode(cookieName); subKey = Encode(subKey); HttpCookie cookie; if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) { cookie = new HttpCookie(cookieName); } else { cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName]; } cookie.Domain = domain; cookie.Path = path; cookie.Values.Remove(subKey); System.Web.HttpContext.Current.Response.Cookies.Add(cookie); } public static string Encode(string input) { if (string.IsNullOrEmpty(input)) { return null; } // return AntiXssEncoderTwo.UrlEncode(input); return HttpUtility.UrlEncode(input);//.Replace("_", "%5F").Replace("&", "%26").Replace("+", "%2B"); } public static string Decode(string input) { return HttpUtility.UrlDecode(input); } #endregion }}
CookieHelper
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。