zoukankan      html  css  js  c++  java
  • CookieHelper

    using System;
    using System.Web;

    namespace MingYu.Utility
    {
    /// <summary>
    /// Cookie帮助类
    /// </summary>
    public class CookieHelper
    {
    /// <summary>
    /// 取Cookie
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static HttpCookie Get(string name)
    {
    return HttpContext.Current.Request.Cookies[name];
    }

    /// <summary>
    /// 取Cookie值
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static string GetValue(string name)
    {
    var httpCookie = Get(name);
    if (httpCookie != null)
    return httpCookie.Value;
    else
    return string.Empty;
    }

    /// <summary>
    /// 取Cookie值
    /// </summary>
    /// <param name="name"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetValue(string name, string key)
    {
    var httpCookie = Get(name);
    if (httpCookie != null)
    return httpCookie.Values[key];
    else
    return string.Empty;
    }

    /// <summary>
    /// 移除Cookie
    /// </summary>
    /// <param name="name"></param>
    public static void Remove(string name)
    {
    Remove(Get(name));
    }

    public static void Remove(HttpCookie cookie)
    {
    if (cookie != null)
    {
    cookie.Expires = DateTime.Now.AddDays(-1);
    Set(cookie);
    }
    }

    /// <summary>
    /// 新增Cookie
    /// </summary>
    /// <param name="name"></param>
    /// <param name="value"></param>
    /// <param name="expiresHours"></param>
    public static void Add(string name, string value, int expiresHours = 0)
    {
    var httpCookie = Get(name);
    if (httpCookie == null)
    httpCookie = Set(name);

    httpCookie.Value = value;
    Add(httpCookie, expiresHours: expiresHours);
    }

    /// <summary>
    /// 新增Cookie
    /// </summary>
    /// <param name="cookie"></param>
    /// <param name="expiresHours"></param>
    public static void Add(HttpCookie cookie, int expiresHours = 0, int minutes = 0)
    {
    string urlHost = HttpContext.Current.Request.Url.Host.ToLower();
    var expiresTime = DateTime.Now;
    if (expiresHours > 0)
    {
    expiresTime = expiresTime.AddHours(expiresHours);
    }
    if (minutes > 0)
    {
    expiresTime = expiresTime.AddMinutes(minutes);
    }
    if (expiresHours > 0 || minutes > 0)
    {
    cookie.Expires = expiresTime;
    }
    HttpContext.Current.Response.Cookies.Add(cookie);
    }

    public static HttpCookie Set(string name)
    {
    return new HttpCookie(name);
    }

    /// <summary>
    /// 更新Cookie
    /// </summary>
    /// <param name="cookie"></param>
    /// <param name="expiresHours"></param>
    public static void Set(HttpCookie cookie, int expiresHours = 0)
    {
    if (expiresHours > 0)
    cookie.Expires = DateTime.Now.AddHours(expiresHours);

    HttpContext.Current.Response.Cookies.Set(cookie);
    }
    }
    }

  • 相关阅读:
    温昱谈架构和框架(Framework)的区别
    温故而知新:HttpApplication,HttpModule,HttpContext及Asp.Net页生命周期
    怎么设计一个好的数据库
    ORACLE修改表空间方法
    为[ double ] 类型 添加[zzzzz]方法
    常用的html代码 加粗 加亮 字型加大 变色等
    ASP.NET页面级别的事务
    无法加载 DLL“oramts.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)
    把漫画浏览器的离线下载的功能给实现了一下
    以前写的IE9鼠标手势插件在IE10下也能工作
  • 原文地址:https://www.cnblogs.com/zlj-rechio/p/cookie.html
Copyright © 2011-2022 走看看