zoukankan      html  css  js  c++  java
  • asp.net 关于cookie的操作

    一、无子键或单级cookie 读写
    (1)、写入:
    第一种
    HttpCookie cookie=new HttpCookie("User");
    cookie.Value="admin";
    cookie.Expires=DateTime.Now.AddMinutes(1000);
    HttpContext.Current.Response.AppendCookie(cookie);
    或者
    HttpContext.Current.Response.Cookies.Add(cookie);

    第二种:
    HttpContext.Current.Response.Cookies['User'].Value="admin";
    HtttpContext.Current.Response.Cookies["User"].Exipres=DateTime.Now.AddMinutes(100);

    (2)、读取
    if(HttpContext.Current.Request.Cookies[key]!=null)
    {
      string value=HttpContext.Current.Request.Cookies[key];
    }
    else
    {
      string value="不存在"+key;
    }
    (3)、修改
    if(HttpContext.Current.Request.Cookies[key]!=null)
    {
      HttpCookie cookie=HttpCookie.Current.Request.Cookies[key];
      cookie.Value=value;
      HttpContext.Current.Response.Cookies.Add(cookie);
    }
    (4)、删除
    if(HttpContext.Current.Request.Cookies[key]!=null)
    {
      HttpCookie cookie=HttpContext.Current.Request.Cookies[key];
      cookie.Expires=DateTime.Now.AddMiuntes(time);//负数
      HttpContext.Current.Response.Cookies.Add(cookie);
    }

    二、有子键或多级cookie 读写

    (1)、创建
    HttpCookie cookie=new HttpCookie("user","admin");
    或者
    HttpCookie cookie=new HttpCookie("user");
    cookie.Value="admin";
    -------------------------------
    cookie.Expires=DateTime.Now.AddMinutes(2);
    cookie.Values["Name"]="Li";

    cookie.Values.Add("Phone","12300000");
    ---------------------------------
    HttpContext.Current.Response.Cookies.Add(cookie);

    (2)、读取
    if(HttpContext.Current.Request.Cookies[key]!=null)
    {
      string value=HttpContext.Current.Request.Cookies[key][subkey] ?? "不存在:"+key+"->"+subkey;
    }
    else
    {
      string value="不存在"+key;
    }

    (3)、修改
    if(HttpContext.Current.Request.Cookies[key]!=null)
    {
      HttpCookie cookie=HttpCookie.Current.Request.Cookies[key];
      cookie[subkey].Value=value;
      HttpContext.Current.Response.Cookies.Add(cookie);
    }

  • 相关阅读:
    spring core源码解读之ASM4用户手册翻译之一asm简介
    nginx启动,重启,关闭命令
    linux LVM分区查看dm设备
    jdbc 对sqlite的基本操作
    linux配置多个ip
    细说Linux下的虚拟主机那些事儿
    打造字符界面的多媒体Linux系统
    linux计划crontab
    因修改/etc/ssh权限导致的ssh不能连接异常解决方法
    Linux修改主机名
  • 原文地址:https://www.cnblogs.com/coderblog/p/9067577.html
Copyright © 2011-2022 走看看