zoukankan      html  css  js  c++  java
  • CookieHelper.cs(20170223)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace System.CRM.Common
    {
        /// <summary>
        /// Cookie操作助手类
        /// </summary>
        public class CookieHelper
        {
            /// <summary>
            /// 设置一个Cookie(一天超时)
            /// </summary>
            /// <param name="key">Cookie的Key值</param>
            /// <param name="value">Cookie的Value值</param>
            public static void SetCookie(string key, string value)
            {
                SetCookie(HttpContext.Current.Response, key, value, DateTime.Now.AddDays(1));
            }
    
            /// <summary>
            /// 设置一个Cookie
            /// </summary>
            /// <param name="key">Cookie的Key值</param>
            /// <param name="value">Cookie的Value值</param>
            /// <param name="expires">Cookie的超时时间</param>
            public static void SetCookie(string key, string value, DateTime expires)
            {
                SetCookie(HttpContext.Current.Response, key, value, expires);
            }
    
            /// <summary>
            /// 移除Cookie
            /// </summary>
            /// <param name="key">Cookie的Key值</param>
            public static void RemoveCookie(string key)
            {
                SetCookie(HttpContext.Current.Response, key, null, DateTime.Now.AddMinutes(-1));
            }
    
            /// <summary>
            /// 获取Cookie值(查)
            /// </summary>
            /// <param name="key">Cookie的Key值</param>
            /// <returns>Cookie的Value值</returns>
            public static string GetCookie(string key)
            {
                return GetCookie(HttpContext.Current.Request, key);
            }
    
            /// <summary>
            /// 设置一个Cookie(增删改)
            /// </summary>
            /// <param name="response">当前 HTTP 响应的 System.Web.HttpResponse 对象</param>
            /// <param name="key">Cookie的Key值</param>
            /// <param name="value">Cookie的Value值</param>
            /// <param name="expires">Cookie的超时时间</param>
            private static void SetCookie(HttpResponse response, string key, string value, DateTime expires)
            {
                HttpCookie cookie = new HttpCookie(key)
                {
                    Value = value,
                    Expires = expires
                };
                response.Cookies.Add(cookie);
            }
    
            /// <summary>
            /// 获取Cookie值(查)
            /// </summary>
            /// <param name="request">当前 HTTP 请求的 System.Web.HttpRequest 对象</param>
            /// <param name="key">Cookie的Key值</param>
            /// <returns>Cookie的Value值</returns>
            private static string GetCookie(HttpRequest request, string key)
            {
                HttpCookie cookie = request.Cookies[key];
                return cookie != null ? cookie.Value : null;
            }
        }
    }
  • 相关阅读:
    arcgis10.4.X的oracle数据库要求
    面试高峰期,如何应对面试官的jvm刁难,特写一篇jvm面经(第一部)
    某公司面试打分文档,75分通过
    git-github 提示Permission denied (publickey) (windows)
    jar项目 BeanDefinitionParsingException: Configuration problem:Unable to locate Spring NamespaceHandler for XML schema namespace
    关于No Spring WebApplicationInitializer types detected on classpath的提示,tomcat 卡主
    spring+orm框架的兼容问题
    程序员常用工具
    IDEA-常见问题
    IDEA-基本设置
  • 原文地址:https://www.cnblogs.com/zyx321/p/6435898.html
Copyright © 2011-2022 走看看