zoukankan      html  css  js  c++  java
  • c# 读写cookie 通用函数

    #region 读取或写入cookie
            ///
            /// 写cookie值
            ///
            /// 名称
            /// 值
            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);
            }
     
            ///
            /// 写cookie值
            ///
            /// 名称
            /// 值
            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);
            }
     
            ///
            /// 写cookie值
            ///
            /// 名称
            /// 值
            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);
            }
     
            ///
            /// 写cookie值
            ///
            /// 名称
            /// 值
            /// 过期时间(分钟)
            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);
            }
     
            ///
            /// 读cookie值
            ///
            /// 名称
            /// cookie值
            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 "";
            }
     
            ///
            /// 读cookie值
            ///
            /// 名称
            /// cookie值
            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
  • 相关阅读:
    Linux 下如何查看一个组内的有哪些用户
    Linux下查看用户列表
    用pureftpd+pureDB虚拟用户,建立一个简单安全(不需要数据库支持)的linux ftp站
    pure-ftp中如何设置匿名帐号,自定义匿名帐号访问指定目录
    PUTTY中永久更改字体大小
    Pure-ftpd 如何配置多用户,且A用户具有上传下载权限,B用户只有下载权限?
    windows10访问ftp中文乱码怎么办?
    WPF DynamicDataDisplay.dll 下载
    C# windows presentation foundation 项目中不支持Application
    c# NDP472-KB4054530-x86-x64-AllOS-CHS下载
  • 原文地址:https://www.cnblogs.com/choii/p/5782109.html
Copyright © 2011-2022 走看看