/// <summary> /// 添加cookie值 /// </summary> /// <param name="name">名称</param> /// <param name="value">值</param> /// <param name="decode">是否加密</param> public static void AddCookie(string name, string value, bool encode) { if (encode) { value = EKEncrypt.EncryptDES(value, _encryptkey); } System.Web.HttpCookie cook = System.Web.HttpContext.Current.Request.Cookies[name]; if (cook == null) { cook = new System.Web.HttpCookie(name); if (!EKRequest.GetHost().Contains("192.168.") && !EKRequest.GetHost().Contains("localhost")) { cook.Domain = EKRequest.GetDoMain(); } cook.Value = value; cook.Expires = DateTime.Now.AddMinutes(_minute); System.Web.HttpContext.Current.Response.Cookies.Add(cook); } else { if (!EKRequest.GetHost().Contains("192.168.") && !EKRequest.GetHost().Contains("localhost")) { cook.Domain = EKRequest.GetDoMain(); } cook.Value = value; cook.Expires = DateTime.Now.AddMinutes(_minute); System.Web.HttpContext.Current.Response.Cookies.Add(cook); } }
/// <summary> /// 重新设置指定cookie值 /// </summary> /// <param name="name">名称</param> /// <param name="value">值</param> /// <param name="encode">是否加密</param> public static void SetCookie(string name, string value, bool encode) { if (System.Web.HttpContext.Current.Request.Cookies == null || System.Web.HttpContext.Current.Request.Cookies[name] == null || System.Web.HttpContext.Current.Request.Cookies[name].Value == null || System.Web.HttpContext.Current.Request.Cookies[name].Value == "") { AddCookie(name, value, encode); return; } System.Web.HttpContext.Current.Response.Cookies[name].Value = encode ? EKEncrypt.EncryptDES(value, _encryptkey) : value; }
/// <summary> /// 获取cookie值,加默认值 /// </summary> /// <param name="name">名称</param> /// <param name="defaultValue">默认值</param> /// <param name="decode">是否解密</param> /// <returns></returns> public static string GetCookie(string name, string defaultValue, bool decode) { if (System.Web.HttpContext.Current.Request.Cookies == null || System.Web.HttpContext.Current.Request.Cookies[name] == null || System.Web.HttpContext.Current.Request.Cookies[name].Value == null || System.Web.HttpContext.Current.Request.Cookies[name].Value == "") { return(defaultValue); } if (decode) { return(EKEncrypt.DecryptDES(System.Web.HttpContext.Current.Request.Cookies[name].Value, _encryptkey)); } return(System.Web.HttpContext.Current.Request.Cookies[name].Value); }