zoukankan      html  css  js  c++  java
  • asp.net中读取、设置、移除Cookie

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Newtonsoft.Json;

    namespace Xys.PetShop.Web
    {
        public class Cookie<T>
        {
            public static void SetCookie(string name, T tValue, DateTime expires)
            {
                string value=JsonConvert.SerializeObject(tValue);
                HttpCookie cookie = new HttpCookie(name, value);
                cookie.Expires = expires;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            public static T GetCookie(string name)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
                if (cookie != null)
                {
                    return JsonConvert.DeserializeObject<T>(cookie.Value);
                }
                else
                    return default(T);
            }

            public static void RemoveCookie(string name)
            {
                HttpCookie cookie = new HttpCookie(name);
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }

  • 相关阅读:
    memcache 基本操作
    PHP 实现定时任务的几种方法
    PDO 事务处理
    mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
    ASP.NET Web API 跨域访问(CORS)
    nmap使用
    买定离手,落子无悔
    html5plus处理返回键
    PAT 1008 数组元素循环右移问题
    PAT 1007 素数对猜想
  • 原文地址:https://www.cnblogs.com/50614090/p/2377234.html
Copyright © 2011-2022 走看看