码迷,mamicode.com
首页 > 其他好文 > 详细

我的cookie读写

时间:2015-09-28 11:42:06      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

前后台必须一致,

后台:

public static void SetCookie(string cookieName, string value, int expiresDays)
{
    var newCookie = new HttpCookie(cookieName);
    newCookie.Value = value;
    newCookie.Expires = DateTime.Now.AddDays(expiresDays);
    newCookie.Path = "/";
    System.Web.HttpContext.Current.Response.AppendCookie(newCookie);
}

public static string GetCookie(string cookieName)
{
    if (System.Web.HttpContext.Current.Request.Cookies[cookieName] != null
        && !string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[cookieName].Value))
    {
        string value = System.Web.HttpContext.Current.Request.Cookies[cookieName].Value;
        return HttpUtility.UrlDecode(value);
    }

    return null;
}

 

调用者:

CookieHelper.SetCookie(ckIdName, "0", 7);

userId = CookieHelper.GetCookie(ckIdName)

前台:

$.cookie(‘productIds‘, "," + productId, { expires: 7, path: ‘/‘ });

必须一致才能进行修改。

我的cookie读写

标签:

原文地址:http://www.cnblogs.com/luminji/p/4843555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!