asp.net core 3.1 Cookie的公共方法


/// <summary>
    /// 沐雪微淘小程序商城
    /// cookie設置
    /// </summary>
    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);
        }
        /// <summary>
        /// 獲取cookies
        /// </summary>
        /// <param name="key"></param>
        /// <returns>返回對應的值</returns>
        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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM