zoukankan      html  css  js  c++  java
  • 提供一个Cookies操作类(支持带域名与不带域名)IP和localhost域名调试

    来看一下COOKIES公用操作类

     
        /// <summary>
        /// Cookie 操作帮助类
        /// </summary>
        public class CookieHelper
        {
     
            #region 写cookie值
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue">值</param>
            public static void Write(string strName, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = strValue;
                HttpContext.Current.Response.AppendCookie(cookie);
     
            }
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue">值</param>
            /// <param name="doMain">域  例如:contoso.com</param>
            public static void WriteWithDomain(string strName, string strValue, string doMain)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = strValue;
                cookie.Domain = doMain;
                HttpContext.Current.Response.AppendCookie(cookie);
     
            }
     
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="key">键名</param>
            /// <param name="strValue">值</param>
            public static void Write(string strName, string key, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = strValue;
                HttpContext.Current.Response.AppendCookie(cookie);
     
            }
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="key">键名</param>
            /// <param name="strValue">值</param>
            /// <param name="doMain">域  例如:contoso.com</param>
            public static void WriteWithDomain(string strName, string key, string strValue
                                               , string doMain)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Domain = doMain;
                cookie[key] = strValue;
                HttpContext.Current.Response.AppendCookie(cookie);
     
            }
     
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue">值</param>
            /// <param name="strValue">过期时间(分钟)</param>
            public static void Write(string strName, string strValue, int expires)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
     
                cookie.Value = strValue;
                cookie.Expires = System.DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue">值</param>
            /// <param name="strValue">过期时间(分钟)</param>
            /// <param name="doMain">域  例如:contoso.com</param>
            public static void WriteWithDomain(string strName, string strValue, int expires
                                                , string doMain)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Domain = doMain;
                cookie.Value = strValue;
                cookie.Expires = System.DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="key">键名</param>
            /// <param name="strValue">值</param>
            /// <param name="expires">过期时间(分钟)</param>
            public static void Write(string strName, string key, string strValue, int expires)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = strValue;
                cookie.Expires = System.DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="key">键名</param>
            /// <param name="strValue">值</param>
            /// <param name="expires">过期时间(分钟)</param>
            /// <param name="doMain">域  例如:contoso.com</param>
            public static void WriteWithDomain(string strName, string key, string strValue
                                               , int expires, string doMain)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Domain = doMain;
                cookie[key] = strValue;
                cookie.Expires = System.DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
     
            #endregion
     
            #region 读cookie值
     
            /// <summary>
            /// 读cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <returns>cookie值</returns>
            public static string Read(string strName)
            {
                if (HttpContext.Current.Request.Cookies != null
                    && HttpContext.Current.Request.Cookies[strName] != null)
                {
                    return HttpContext.Current.Request.Cookies[strName].Value;
                }
                else
                {
                    return string.Empty;
                }
            }
     
            /// <summary>
            /// 读cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="key">键名</param>
            /// <returns>cookie值</returns>
            public static string Read(string strName, string key)
            {
                if (HttpContext.Current.Request.Cookies != null
                    && HttpContext.Current.Request.Cookies[strName] != null)
                {
                    return HttpContext.Current.Request.Cookies[strName][key];
                }
                else
                {
                    return string.Empty;
                }
            }
     
            #endregion
     
            #region Cookie 删除
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="name">名称</param>
            public static void Remove(string name)
            {
                if (HttpContext.Current.Request.Cookies != null
                    && HttpContext.Current.Request.Cookies[name] != null)
                {
                    HttpCookie myCookie = new HttpCookie(name);
                    myCookie.Expires = DateTime.Now.AddMinutes(-1);
                    HttpContext.Current.Response.Cookies.Add(myCookie);
                }
            }
     
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="name">名称</param>
            /// <param name="key">二级建名称</param>
            public static void Remove(string name, string key)
            {
                if (HttpContext.Current.Request.Cookies != null
                    && HttpContext.Current.Request.Cookies[name] != null
                    && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[name][key]))
                {

    string[] temp = HttpContext.Current.Request.Cookies[name].Value

    .Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);

                    List<string> list = new List<string>();
                    foreach (string item in temp)
                    {
                        if (item.StartsWith(key))
                        {
                            continue;
                        }
                        else
                        {
                            list.Add(item);
                        }
                    }
                    Write(name, string.Join("&", list.ToArray()));
                }
            }
            #endregion
     
        
        }

    而我们通过这个公用类来对cookies操作就显得容易了许多

     public void Write()
            {
                //不用域名,通用性
                VCommons.Http.CookieHelper.Write("test", "name", "bobo123");
                //加域名,IP地址形式
                VCommons.Http.CookieHelper.WriteWithDomain("test2", "nameByDomain"
                                                          , "bobo>Domain>IP", "127.0.0.1");
                //加域名,可以用localhost进行本地调试
                VCommons.Http.CookieHelper.WriteWithDomain("test3", "nameByDomain"
                                                           , "bobo>localhost", "localhost");
     
            }
            public void Clear()
            {
     
                //清除cookies名称为test的所有cookies元素            
                VCommons.Http.CookieHelper.Remove("test");

    //将指定名称下的指定键值设置成空

    VCommons.Http.CookieHelper.Write("test", "name", string.Empty);

                VCommons.Http.CookieHelper.Write("test2", "nameByDomain", string.Empty);
                VCommons.Http.CookieHelper.Write("test3", "nameByDomain", string.Empty);
     
            }
  • 相关阅读:
    个人永久性免费-Excel催化剂功能第31波-数量金额分组凑数功能,财务表哥表姐最爱
    个人永久性免费-Excel催化剂功能第30波-工作表快捷操作(批量创建、命名、排序、工作表目录)
    个人永久性免费-Excel催化剂功能第29波-追加中国特色的中文相关自定义函数
    发现用System.Net.Mail发邮件(代码附后),附件稍微大一点就会造成程序假死. 有没有什么简单的解决办法呢? 多谢!!
    上传文件 获取文件名 --360浏览器
    js中的json对象和字符串之间的转化
    chosen.jquery.js
    select 后台获取text 并根据text 设置value选中项
    iframe中有ajax,设置iframe自适应高度
    charme浏览器 jquery1.9.1min.js 报脚本错误 无jquery.min.map 文件
  • 原文地址:https://www.cnblogs.com/lori/p/2136031.html
Copyright © 2011-2022 走看看