• C# CookieExtension 使用Cookie的扩展工具类


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

    namespace Common
    {
        /// <summary>
        /// 用于方便使用Cookie的扩展工具类
        /// </summary>
        public static class CookieExtension
        {
            /// <summary>
            /// 从一个Cookie中读取字符串值。
            /// </summary>
            /// <param name="cookie"></param>
            /// <returns></returns>
            public static string GetString(this HttpCookie cookie)
            {
                if (cookie == null)
                    return null;

                return cookie.Value;
            }


            /// <summary>
            /// 从一个Cookie中读取值并转成指定的类型
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="cookie"></param>
            /// <returns></returns>
            public static T ConverTo<T>(this HttpCookie cookie)
            {
                if (cookie == null)
                    return default(T);

                return (T)Convert.ChangeType(cookie.Value, typeof(T));
            }

            /// <summary>
            /// 从一个Cookie中读取【JSON字符串】值并反序列化成一个对象,用于读取复杂对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="cookie"></param>
            /// <returns></returns>
            public static T FromJson<T>(this HttpCookie cookie)
            {
                if (cookie == null)
                    return default(T);
                return JsonData.FromJson<T>(HttpUtility.UrlDecode(cookie.Value));
            }


            /// <summary>
            /// 将一个对象写入到Cookie
            /// </summary>
            /// <param name="obj">对象</param>
            /// <param name="name">名称</param>
            /// <param name="expries">过期时间</param>
            public static void WriteCookie(this object obj, string name, DateTime? expries)
            {
                if (obj == null)
                    throw new ArgumentNullException("obj");

                if (string.IsNullOrEmpty(name))
                    throw new ArgumentNullException("name");


                HttpCookie cookie = new HttpCookie(name, HttpUtility.UrlEncode(obj.ToString()));

                if (expries.HasValue)
                    cookie.Expires = expries.Value;

                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            /// <summary>
            /// 删除指定的Cookie
            /// </summary>
            /// <param name="name"></param>
            public static void ClaerCookie(string name)
            {
                if (string.IsNullOrEmpty(name))
                    throw new ArgumentNullException("name");

                HttpCookie cookie = new HttpCookie(name);

                // 删除Cookie,其实就是设置一个【过期的日期】
                cookie.Expires = new DateTime(1900, 1, 1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }

  • 相关阅读:
    arcgis要素转json
    GeoJSON.js:221 Uncaught Error: Invalid GeoJSON object.
    深入学习SAP UI5框架代码系列之一:UI5 Module的懒加载机制
    SAP Spartacus简介
    金庸逝世两周年纪念日:一个失意程序员的呓语
    一个用于SAP UI5学习的脚手架应用,没有任何后台API的依赖
    通过最简单的button控件,深入学习SAP UI5框架代码系列之零
    如何使用SAP UI5 SDK网站查询指定控件的属性如何使用
    一行代码将SAP CDS view数据以ALV的方式输出
    演讲预告:一个月的住院经历,我悟到了哪些和程序员职场发展相关的心得
  • 原文地址:https://www.cnblogs.com/weixing/p/2548767.html
Copyright © 2020-2023  润新知