asp.net core 3.1 Cookie的公共方法

2021-01-19 08:12

阅读:389

标签:www   add   bsp   http   方法   request   rem   设置   app   

/// 
    /// 沐雪微淘小程序商城
    /// cookie设置
    /// 
    public static class CookieHelper
    {
        private static HttpResponse CurrentResponse
        {
            get
            {
                return AppHttpContext.Current.Response;
            }
        }
        private static HttpRequest CurrentRequest
        {
            get
            {
                return AppHttpContext.Current.Request;
            }
        }
        public static void Add(string key, string value, int minutes = 30)
        {
            CurrentRequest.Cookies.TryGetValue(key, out string tmpvalue);
            if (!string.IsNullOrEmpty(tmpvalue))
            {
                CurrentResponse.Cookies.Delete(key);
            }
            CurrentResponse.Cookies.Append(key, value, new CookieOptions
            {
                Expires = DateTime.Now.AddMinutes(minutes)
            });
        }

        public static void Delete(string key)
        {
            CurrentResponse.Cookies.Delete(key);
        }
        /// 
        /// 获取cookies
        /// 
        /// 
        /// 返回对应的值
        public static string GetCookies(string key)
        {
            CurrentRequest.Cookies.TryGetValue(key, out string value);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;
            return value;

        }

代码如上,非常简单;AppHttpContext的封装,请看我上一篇文章https://www.cnblogs.com/puzi0315/p/13337279.html。

 

asp.net core 3.1 Cookie的公共方法

标签:www   add   bsp   http   方法   request   rem   设置   app   

原文地址:https://www.cnblogs.com/puzi0315/p/13337289.html


评论


亲,登录后才可以留言!