zoukankan      html  css  js  c++  java
  • asp.net Cookie的用法实例

        #region cookie操作
        /// <summary>
        /// 读取cookie
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static string GetCookie(string keys)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["domain"];
            if (cookie != null)
            {
                return cookie.Values[keys].ToString();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 为cookie赋值
        /// </summary>
        /// <param name="strNameTag"></param>
        /// <param name="strValue"></param>
        public static void GetCookie(string keys, string values)
        {
            if (HttpContext.Current.Request.Cookies["domain"] == null)
            {
                HttpCookie cookies = new HttpCookie("domain");//定义cookie对象
                cookies.Values[keys] = values;
                DateTime dts = DateTime.Now;//定义时间对象
                TimeSpan ts = new TimeSpan(0, 0, 20, 0);//cookie有效作用时间
                cookies.Expires = dts.Add(ts);//添加作用时间
                HttpContext.Current.Response.Cookies.Add(cookies);
            }
            else
            {
                HttpCookie cookies = HttpContext.Current.Request.Cookies["domain"];
                cookies.Values[keys] = values;
                DateTime dts = DateTime.Now;//定义时间对象
                TimeSpan ts = new TimeSpan(0, 0, 20, 0);//cookie有效作用时间,具体查msdn
                cookies.Expires = dts.Add(ts);//添加作用时间
                HttpContext.Current.Response.Cookies.Add(cookies);
            }
        }
        /// <summary>
        /// 清除cookies
        /// </summary>
        public static void clearCookie()
        {
            if (HttpContext.Current.Request.Cookies["domain"] != null)
            {
                HttpCookie cookies = HttpContext.Current.Request.Cookies["domain"];
                cookies.Values.Clear();
                DateTime dts = DateTime.Now;//定义时间对象
                TimeSpan ts = new TimeSpan(0, 0, 0, 0);//cookie有效作用时间
                cookies.Expires = dts.Add(ts);//添加作用时间
                HttpContext.Current.Response.Cookies.Add(cookies);
            }
        }
        #endregion
    多思考,多创新,才是正道!
  • 相关阅读:
    IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET
    mysql 存储过程 循环
    Mysql 遇到过的自带函数使用
    Mysql:is not allowed to connect to this MySQL server
    mysql 删除或更新时出现 you are usering safe update model 的解决方案
    将一个服务器的表数据插入到另一个服务器的表中
    C#操作Excel无法删除worksheet解决方案
    安装MongoDB数据库的注意事项
    Python爬取小猪短租,用的是lxml解析器
    使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ​​​​
  • 原文地址:https://www.cnblogs.com/shuang121/p/1966355.html
Copyright © 2011-2022 走看看