using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Maticsoft.DBUtility
{
    public class CookieHelper
    {
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public static bool setCookie(string name, string value, int day)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(name);
                cookie.Expires = DateTime.Now.AddDays(day);
                cookie.Value = value;
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string getCookie(string name)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie != null)
            {
                return cookie.Value.ToString();
            }
            return null;
        }
        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool delCookie(string name)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(name);
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
  • 2022-03-02
  • 2021-05-17
  • 2021-07-15
  • 2021-06-30
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2021-07-15
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案