Cookie

/// <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);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末崎溃,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子盯质,更是在濱河造成了極大的恐慌笨奠,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唤殴,死亡現(xiàn)場離奇詭異,居然都是意外死亡到腥,警方通過查閱死者的電腦和手機朵逝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乡范,“玉大人配名,你說我怎么就攤上這事〗荆” “怎么了渠脉?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長瓶佳。 經(jīng)常有香客問我芋膘,道長,這世上最難降的妖魔是什么霸饲? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任为朋,我火速辦了婚禮,結(jié)果婚禮上厚脉,老公的妹妹穿的比我還像新娘习寸。我一直安慰自己,他們只是感情好傻工,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布霞溪。 她就那樣靜靜地躺著孵滞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鸯匹。 梳的紋絲不亂的頭發(fā)上坊饶,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音忽你,去河邊找鬼幼东。 笑死,一個胖子當著我的面吹牛科雳,可吹牛的內(nèi)容都是我干的根蟹。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼糟秘,長吁一口氣:“原來是場噩夢啊……” “哼简逮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起尿赚,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤散庶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后凌净,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悲龟,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年冰寻,在試婚紗的時候發(fā)現(xiàn)自己被綠了须教。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡斩芭,死狀恐怖轻腺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情划乖,我是刑警寧澤贬养,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站琴庵,受9級特大地震影響误算,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜迷殿,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一尉桩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贪庙,春花似錦蜘犁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奏窑。三九已至,卻和暖如春屈扎,著一層夾襖步出監(jiān)牢的瞬間埃唯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工鹰晨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留墨叛,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓模蜡,卻偏偏與公主長得像漠趁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子忍疾,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容