/// <summary>
/// CookieHelper操作工具类
/// </summary>
public class CookieHelper
{
/// <summary>
/// 设置cookie值
/// </summary>
/// <param name="cookiename">cookie名称</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="expires">失效期</param>
public static void WriteCookie(string cookiename, string key, string val, DateTime expires)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
HttpCookie cookie = Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = ConfigHelper.SiteLoginDomain;
}
if (val == null)
{
cookie.Values.Remove(key);
}
else
{
cookie.Values[key] = val;
cookie.Expires = expires;
}
Response.Cookies.Add(cookie);
}
public static void WriteCookie(string cookiename, string key, string val, int expires)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
HttpCookie cookie = Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = ConfigHelper.SiteLoginDomain;
}
if (val == null)
{
cookie.Values.Remove(key);
}
else
{
cookie.Values[key] = val;
cookie.Expires = DateTime.Now.AddMinutes(expires);
}
Response.Cookies.Add(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string key, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie[key] = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="strValue">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
return "";
}
/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName, string key)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
return HttpContext.Current.Request.Cookies[strName][key].ToString();
return "";
}
/// <summary>
/// 移除指定名称的cookie对象中的集合对
/// </summary>
/// <param name="cookieName">cookie名称</param>
public static void RemoveCookie(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Clear();
cookie.Domain = ConfigHelper.SiteLoginDomain;
cookie.Expires = DateTime.Now.AddDays(-10000d);
Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 移除指定名称的cookie对象中的集合中的键值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
public static void RemoveCookieKey(string cookieName, string key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Remove(key);
Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 移除指定名称的cookie对象中的集合中的键值
/// </summary>
/// <param name="cookieName">cookie名称</param>
/// <param name="index">键值名</param>
public static void RemoveCookieKey(string cookieName, int index)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
HttpResponse Response = HttpContext.Current.Response;
if (cookie != null)
{
cookie.Values.Remove(cookie.Values.GetKey(index));
Response.Cookies.Add(cookie);
}
}
}
Cookie
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 一、背景介绍 域名是什么? 要解释域名是什么,我们应当知道一些互联网的基本概念 我们实现互联网的方式中有一...
- 一.背景介绍 1.什么是IP地址 互联网协议地址(英语:Internet Protocol Address,又译为...
- JS 设置cookie、读取cookie、删除cookie 通常浏览器在第一次页面加载时候会帮我们把网站的图片数据...
- 这几天,APP需要添加一组动画,然后设计同事用spine导出了一些动画给我,下面就是我的探索添加动画的过程。 有几...
