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
  • 相关阅读:
    自适应兄弟元素一起增加高度
    replace小坑位一个
    word-wrap: break-word word-break: break-all;
    1473B. String LCM
    A. Special Permutation(水题)
    B. BerSU Ball(贪心)
    A. Regular Bracket Sequence(水题)
    B. Strange List(数学题)
    C. Move Brackets(水题)
    A. Flipping Game(暴力求法)
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496262.html
Copyright © 2011-2022 走看看