定义 CookieHelper 工具类缓存和获取Cookie信息

定义 CookieHelper 工具类缓存和获取Cookie信息

 public class CookieHelper
    {
        //缓存Cookie信息
        public static void Set(string strName, string strValue)
        {
            HttpCookie _cookie;
            try
            {
                _cookie = new HttpCookie(strName);
                _cookie.Value = strValue;
                _cookie.Expires = DateTime.Now.AddMonths(1);
                HttpContext.Current.Response.Cookies.Add(_cookie);
            }
            catch (Exception ex)
            {

            }
        }

        //获取Cookie信息
        public static string Get(string strName)
        {
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie != null)
                    return cookie.Value.ToString();
                else
                    return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }
    }

 

posted @ 2021-05-12 10:13  码农阿亮  阅读(94)  评论(0编辑  收藏  举报