zoukankan      html  css  js  c++  java
  • C#操作Cookie

    /* 创建者:菜刀居士的博客
     * 创建日期: 2014年09月02号
     * 功能:操作Cookie
     *
     */

    namespace Net.String.ConsoleApplication
    {
        using System;
        using System.Web;

        public static class CookieHelper
        {
            /// <summary>
            /// 加入cookie
            /// </summary>
            public static void AddCookie(this HttpContext h,string name, string value)
            {
                HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
                h.Response.Cookies.Add(cookieName);
            }

            /// <summary>
            /// 加入cookie
            /// </summary>
            public static void AddCookie(this HttpContext h,string name, string value, TimeSpan span)
            {
                HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));

                cookieName.Expires = DateTime.Now.Add(span);

                h.Response.Cookies.Add(cookieName);
            }

            /// <summary>
            /// 得到cookie
            /// </summary>
            public static string GetCookie(this HttpContext h, string name)
            {
                if (h.Request.Cookies[name] != null)
                {
                    if (h.Response.Cookies.Count > 0 && h.Response.Cookies[name] != null)
                    {
                        return System.Web.HttpUtility.UrlDecode(h.Response.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
                    }
                    return System.Web.HttpUtility.UrlDecode(h.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
                }
                else
                { return string.Empty; }
            }

            /// <summary>
            /// 删除cookie
            /// </summary>
            public static void RemoveCookie(this HttpContext h,string name)
            {
                h.Response.Cookies[name].Value = null;
                h.Response.Cookies[name].Expires = DateTime.Now.AddDays(-1);
            }

            /// <summary>
            /// 清空cookie
            /// </summary>
            public static void ClearCookie(this HttpContext h)
            {
                try
                {
                    foreach (HttpCookie hc in h.Response.Cookies)
                    {
                        hc.Value = null;
                        hc.Expires = DateTime.Now.AddDays(-1);
                    }
                }
                catch { }
            }
        }
    }

  • 相关阅读:
    Linux内存管理 【转】
    Linux内核源码分析--内核启动之(2)Image内核启动(汇编部分)(Linux-3.0 ARMv7) 【转】
    Linux内核源码分析--内核启动之(1)zImage自解压过程(Linux-3.0 ARMv7) 【转】
    Linux的软中断处理实现 【转】
    u_boot移植之内存基础知识DDR【转】
    linux内存管理-内核用户空间 【转】
    底板芯片组与内存映射(Motherboard Chipsets and the Memory Map) 【转】
    深入理解C语言的函数调用过程 【转】
    JAVA学习第三十六课(经常使用对象API)— Set集合:HashSet集合演示
    二叉链表的建立和遍历 完整的前,中,后和层序建立和遍历
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5085554.html
Copyright © 2011-2022 走看看