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);
    }

  • 相关阅读:
    简单的MsChart使用与遇到的麻烦
    SQLServer中case when 与列拼接
    关于集成单点登录问题
    IIS部署网站后,只有本服务器才能登录
    获取本周的周一日期与本周的周日日期
    34个漂亮的应用程序后台管理系统界面(系列二)
    2011年最佳免费 PSD 用户界面素材揭晓
    编程你使用快捷键了吗?
    汉字转全拼音函数优化方案(SQLServer),值得你看看
    WinForm企业应用框架设计【四】动态创建业务窗体
  • 原文地址:https://www.cnblogs.com/coderblog/p/9067577.html
Copyright © 2011-2022 走看看