zoukankan      html  css  js  c++  java
  • C#操作cookie

    创建Cookie

                       HttpCookie cookie = new HttpCookie("MyCook");//初使化并设置Cookie的名称
                        DateTime dt = DateTime.Now;
                        TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0);//过期时间为1天
                        cookie.Expires = dt.Add(ts);//设置过期时间
                        cookie.Values.Add("uid", uid);  //添加cookie
                        Response.AppendCookie(cookie);

    读取Cookie

             if (Request.Cookies["MyCook"] != null)
                {
                    ViewData["Uid"] = Request.Cookies["MyCook"]["uid"]; //输出cookie
                }

    修改Cookie

     //获取客户端的Cookie对象
        HttpCookie cookie= Request.Cookies["MyCook"];
            
        if (cookie!= null)
        {
          //修改Cookie的两种方法
          cookie.Values["uid"] = "134626655";
          cookie.Values.Set("userid", "134626655");
    
          //往Cookie里加入新的内容
          cookie.Values.Set("newid", "newValue");
          Response.AppendCookie(cookie);
        }      

    删除Cookie

    HttpCookie cookie= Request.Cookies["MyCook"];
        if (cookie!= null)
        {
          if (!CheckBox1.Checked)
          {
            cookie.Values.Remove("uid");//移除键值为uid的值
          }
          else
          {
            TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
            cookie.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在
          }
          Response.AppendCookie(cookie);
        }

    设置Cookie过期的时间的TimeSpan

    [Serializable]
    public TimeSpan(
       int days,
       int hours,
       int minutes,
       int seconds
    );
  • 相关阅读:
    Android学习路径(两)项目文件本身使用场景和文件演示
    A左右ndroid正在使用Uri监视数据库中的更改
    离PACKET_INp获取信息acket data
    curl 命令
    POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
    Linux 下一个 Mysql error 2002 错误解决
    图片打水印 缩放 和一个输入流的转换
    qt Qt5开发
    qt 关于Qt中MVC的介绍与使用
    qt mvc3
  • 原文地址:https://www.cnblogs.com/llxy/p/4078563.html
Copyright © 2011-2022 走看看