当前位置:首页 > 编程教程 > asp.net教程 > 实现Cookie集合

实现Cookie集合

2017-10-12 19:45:36[asp.net教程]点击数:作者:Insus.NET来源: 网络
随机为您推荐的文章:.net之微信企业号开发(三) 回调模式的接口开发

这里讲述的是.net之微信企业号开发(三) 回调模式的接口开发的文章,具体方法请看介绍一、前言   微信企业号应用中,有两种模式,一种是普通模式,这种模式只能进行简单网页链接,以

此文描述了实现Cookie集合的说明,具体操作请看面的内容

以前Insus.NET有在博客上有写过一篇《在程序中使用Cookie集合》http://www.cnblogs.com/insus/archive/2011/05/25/2055531.html

昨天Insus.NET有在命名用这方法的专项中,进行改写与优化。

把Cookie类抽出并独立起来。

实现Cookie集合

实现Cookie集合
 private static HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }

        private static HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }
Source Code

这个cookie集合,当然存取单独cookie也行才对:

实现Cookie集合

实现Cookie集合
 public static string GetCookie(string cookieName)
        {
            return Request.Cookies[cookieName] == null ? "" : Request.Cookies[cookieName].Value;
        }

        public static void SetCookie(string cookieName, string value)
        {
            Response.Cookies[cookieName].Value = value;
        }

        public static void RemoveCookie(string cookieName)
        {
            HttpCookie cookie = Request.Cookies[cookieName];
            cookie.Expires = DateTime.Now.AddTicks(-1);
            Response.Cookies.Add(cookie);
        }
Source Code

下面是存储cookie集合,即是说单个cookie中,能够存储多个键值:
实现Cookie集合


实现Cookie集合
  private static Dictionary<string, object> GetCookies(string cookieName)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            if (Request.Cookies[cookieName] != null)
            {
                HttpCookie cookie = Request.Cookies[cookieName];
                if (cookie != null)
                {
                    NameValueCollection nvc = cookie.Values;
                    foreach (string key in nvc.AllKeys)
                    {
                        dic.Add(key, cookie[key]);
                    }
                }
            }
            return dic;
        }

        public static object GetCookies(string cookieName, string keyName)
        {
            Dictionary<string, object> dic = GetCookies(cookieName);
            if (dic.Count > 0 && dic.ContainsKey(keyName))
            {
                return dic[keyName];
            }

            return null;
        }

        public static void SetCookies(string cookieName, Dictionary<string, object> dic)
        {
            HttpCookie cookie = Request.Cookies[cookieName] == null ? new HttpCookie(cookieName) : Request.Cookies[cookieName];
            foreach (KeyValuePair<string, object> val in dic)
            {
                cookie[val.Key] = val.Value.ToString();
            }
            Response.Cookies.Add(cookie);
        }

        public static void RemoveCookies(string cookieName)
        {
            HttpCookie cookie = Request.Cookies[cookieName];
            cookie.Expires = DateTime.Now.AddTicks(-1);
            Response.AppendCookie(cookie);
        }

        public static void RemoveCookies(string cookieName, string keyName)
        {
            HttpCookie cookie = Request.Cookies[cookieName];
            if (cookie != null)
                cookie.Values.Remove(keyName);
            Response.AppendCookie(cookie);
        }
Source Code



以上只是实现一个cookie类。真正在程序中,我们怎样去写cookie呢?也就是说如何使用。你可以写每一个cookie写成一个property,如下:
实现Cookie集合


你可以写成任一个想实现的cookie。以上只是简单写2个属性,一个方法。

在网页中,就可以轻松使用:
赋值:
InsusBase.SystemLoginOk = true;
InsusBase.SystemId = "Key001";


获取值:
Response.Write(InsusBase.SystemLoginOk);
Response.Write(InsusBase.SystemId);



通过本文的学习希望对您了解和学习.net编程的相关知识有一些好的帮助.感谢关注织梦者.我们将为您收集更多更好的.net教程.

这些内容可能对你也有帮助

更多asp.net教程可查看asp.net教程列表页。

TAGS: Cookie