Cookie

/// 
/// CookieHelper操作工具类
/// 
public class CookieHelper
{
    /// 
    /// 设置cookie值
    /// 
    /// cookie名称
    /// 键
    /// 值
    /// 失效期
    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);
    }

    /// 
    /// 写cookie值
    /// 
    /// 名称
    /// 值
    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);
    }

    /// 
    /// 写cookie值
    /// 
    /// 名称
    /// 值
    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);
    }

    /// 
    /// 写cookie值
    /// 
    /// 名称
    /// 值
    /// 过期时间(分钟)
    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);
    }

    /// 
    /// 读cookie值
    /// 
    /// 名称
    /// cookie值
    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 "";
    }

    /// 
    /// 读cookie值
    /// 
    /// 名称
    /// cookie值
    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 "";
    }

    /// 
    /// 移除指定名称的cookie对象中的集合对
    /// 
    /// cookie名称
    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);
        }
    }

    /// 
    /// 移除指定名称的cookie对象中的集合中的键值
    /// 
    /// 
    /// 
    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);
        }
    }

    /// 
    /// 移除指定名称的cookie对象中的集合中的键值
    /// 
    /// cookie名称
    /// 键值名
    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)