zoukankan      html  css  js  c++  java
  • c# Cookie的操作

    #region Cookie的操作:Cookie的读取、Cookie的保存
        /// <summary>
        /// 静态方法:保存Cookie
        /// </summary>
        /// <param name="Hash">内容</param>
        /// <param name="_remberme">存cookie天数</param>
        /// <param name="_CookieName">cookie名</param>
        public static void SaveCookie(Hashtable Hash, int _remberme, string _CookieName)
        {
            string DoMain = "";
            string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
            for (int i = 1; i < Arr.Length; i++)
            {
                DoMain += "." + Arr[i];
            }
            if (_CookieName == "")
            {
                _CookieName = ConfigurationManager.AppSettings["CookieName"];
            }
            HttpCookie Cookie = new HttpCookie(_CookieName);
            foreach (DictionaryEntry entry in Hash)
            {
                Cookie[entry.Key.ToString()] = entry.Value.ToString();
            }
            //Cookie.Domain = ConfigurationManager.AppSettings["CookieDoMain"];
            Cookie.Expires = DateTime.UtcNow.AddDays(_remberme);
            Cookie.Domain = DoMain;
            HttpContext.Current.Response.Cookies.Add(Cookie);
        }

        /// <summary>
        /// 静态方法:读取Cookie的值
        /// </summary>
        /// <param name="_name">Cookie  key</param>
        /// <returns></returns>
        public static string GetCookie(string _name)
        {
            HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(ConfigurationManager.AppSettings["CookieName"]);
            if (Cookie != null)
            {
                string DoMain = "";
                string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
                for (int i = 1; i < Arr.Length; i++)
                {
                    DoMain += "." + Arr[i];
                }
                Cookie.Domain = DoMain;
                return Cookie[_name];
            }
            else
            {
                return "";
            }

        }

        /// <summary>
        /// 静态方法:读取Cookie的值
        /// </summary>
        /// <param name="_name">Cookie  key</param>
        /// <param name="_CookieName">cookie名</param>
        /// <returns></returns>
        public static string GetCookie(string _name, string _CookieName)
        {
            if (_CookieName == "")
            {
                _CookieName = ConfigurationManager.AppSettings["CookieName"];
            }
            HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(_CookieName);
            if (Cookie != null)
            {
                string DoMain = "";
                string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
                for (int i = 1; i < Arr.Length; i++)
                {
                    DoMain += "." + Arr[i];
                }
                Cookie.Domain = DoMain;
                return Cookie[_name];
            }
            else
            {
                return "";
            }

        }
        #endregion

  • 相关阅读:
    mac使用vnc远程登录ubuntu16.04桌面
    last的用法
    MAC笔记本安装telnet
    lsyncd自动同步配置
    四则运算——安卓版
    敏捷开发方法综述
    数组2——数组首尾相接,求最大子数组
    数组1——求一个数组的最大子数组
    《构建之法》阅读笔记04
    学习进度条——第四周
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1785855.html
Copyright © 2011-2022 走看看