您的位置:首页 > 编程语言 > C#

c# 写入读取Cookie值

2014-03-25 14:14 369 查看



Code

/// <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 "";

         }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: