zoukankan      html  css  js  c++  java
  • .net工具类——读取写入cookie

            #region 读取或写入cookie
    
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue"></param>
            public static void WriteCookie(string strName, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = UrlEncode(strValue);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue"></param>
            public static void WriteCookie(string strName, string key, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = UrlEncode(strValue);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue"></param>
            public static void WriteCookie(string strName, string key, string strValue, int expires)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = UrlEncode(strValue);
                cookie.Expires = DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue"></param>
            /// <param name="expires">过期时间(分钟)</param>
            public static void WriteCookie(string strName, string strValue, int expires)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = UrlEncode(strValue);
                cookie.Expires = DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    
            /// <summary>
            /// 读cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <returns>cookie值</returns>
            public static string GetCookie(string strName)
            {
                if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
                    return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
                return "";
            }
    
            /// <summary>
            /// 读cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <returns>cookie值</returns>
            public static string GetCookie(string strName, string key)
            {
                if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
                    return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
    
                return "";
            }
    
            #endregion
  • 相关阅读:
    07.数组和方法
    05.运算符
    04.变量和常量
    专访Vue作者尤雨溪:Vue CLI 3.0重构的原因
    深入理解JavaScript的设计模式
    如何用纯 CSS 创作文本滑动特效的 UI 界面
    如何用纯 CSS 为母亲节创作一颗像素画风格的爱心
    如何用纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
    如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效
    如何用纯 CSS 创作炫酷的同心矩形旋转动画
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496262.html
Copyright © 2011-2022 走看看