zoukankan      html  css  js  c++  java
  • .net Cookie的操作

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    namespace Zhong.Core
    {
        /// <summary>
        /// Cookie操作类
        /// </summary>
        public class CookieHelper
        {
            private static readonly string CookieName = "Zhong";
            /// <summary>
            /// 设置Cookie
            /// </summary>
            /// <param name="name">名称</param>
            /// <param name="values">键/值对</param>
            /// <param name="expires">过期超时时间(秒),为0时不设置过期时间</param>
            /// <param name="domain">域名</param>
            /// <param name="path">路径</param>
            public static void SetCookie(string name, Dictionary<string, string> values, int expires, string domain = null, string path = null)
            {
                HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
                if (cookie == null)
                {
                    cookie = new HttpCookie(name);
                }
                foreach (KeyValuePair<string, string> kv in values)
                {
                    cookie.Values.Add(kv.Key, kv.Value);
                }
                if (domain != null)
                {
                    cookie.Domain = domain;
                }
                if (path != null)
                {
                    cookie.Path = path;
                }
                if (expires != 0)
                {
                    cookie.Expires = DateTime.Now.AddSeconds(expires);  //过期时间
                }
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            /// <summary>
            /// 设置Cookie
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void SetCookie(string key, string value)
            {
                SetCookie(CookieName, new Dictionary<string, string> { { key, value } }, 0);
            }
            /// <summary>
            /// 根据名称与键读取Cookie
            /// </summary>
            /// <param name="name">名称</param>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string GetCookie(string name, string key)
            {
                string returnVal = null;
                HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
                if (cookie != null)
                {
                    returnVal = cookie[key];
                }
                return returnVal;
            }
            /// <summary>
            /// 根据键读取Cookie
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string GetCookie(string key)
            {
                return GetCookie(CookieName, key);
            }
            /// <summary>
            /// 根据名称获取Cookie
            /// </summary>
            /// <param name="name">名称</param>
            /// <returns></returns>
            public static string GetCookieByName(string name)
            {
                string returnVal = null;
                HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
                if (cookie!= null)
                {
                    returnVal = cookie.Value;
                }
                return returnVal;
            }
            /// <summary>
            /// 删除Cookie
            /// </summary>
            /// <param name="name">名称</param>
            public static void DeleteCookie(string name)
            {
                HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddYears(-1);
                    cookie.Values.Clear();
                }
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
  • 相关阅读:
    Getting Started with LINQ in C# 章节概况
    LA 2572 Viva Confetti (Geometry.Circle)
    uva 10652 Board Wrapping (Convex Hull, Easy)
    poj 2743 && LA 3403 Mobile Computing (mideasy Search)
    poj 3525 Most Distant Point from the Sea (DC2 + Half Plane)
    poj 3134 && LA 3621 Power Calculus (迭代加深深度优先搜索)
    LA 4728 Squares (二维凸包+旋转卡壳)
    uva 10256 The Great Divide (Convex Hull, Simple)
    hdu 2454 Degree Sequence of Graph G
    poj 1041 John's trip (Euler Circuit)
  • 原文地址:https://www.cnblogs.com/godbell/p/7190987.html
Copyright © 2011-2022 走看看