zoukankan      html  css  js  c++  java
  • .net操作cookies

    1.创建Cookies方法 

    if (Request.Cookies["Products"] == null)        //判断Products(cookies)是否存在
                    {
                        HttpCookie oCookie = new HttpCookie("Products");
                        oCookie.Expires = DateTime.Now.AddHours(24);      //设置Products(cookies)过去时间
                        oCookie.Value = pro_id.ToString();
                        Response.Cookies.Add(oCookie);
                    }
                    else
                    {
                        bool bExists = false;
                        char[] sep = { ',' };
                        HttpCookie oCookie = (HttpCookie)Request.Cookies["Products"];   //实例cookie
                        oCookie.Expires = DateTime.Now.AddHours(24);    //设置Products(cookies)过去时间
                        string sProdID = oCookie.Value.ToString();      //Products(cookies)包含的值
                        string[] arrCookie = sProdID.Split(sep);

                        for (int i = 0; i < arrCookie.Length; i++)
                        {
                            if (arrCookie[i].Trim() == pro_id.ToString().Trim())
                            {
                                bExists = true;
                            }
                        }
                        if (!bExists)
                        {
                            if (oCookie.Value.Length == 0)
                            {
                                oCookie.Value = pro_id.ToString();
                            }
                            else
                            {
                                oCookie.Value = oCookie.Value + "," + pro_id;
                            }
                        }
                        Response.Cookies.Add(oCookie);      //Products(cookies)添加进cookies
                    }

    2.清除cookies方法

    HttpCookie cookie = Request.Cookies["cookieNmae"];
            cookie.Expires = DateTime.Today.AddDays(-1);

            //重要: 
            Response.Cookies.Add(cookie);

    本文内容来源:互联网博客

  • 相关阅读:
    linux Chrome 安装
    jQuery.extend 函数详解
    Ubuntu Linux 下文件名乱码(无效的编码)的快速解决办法
    Django——model字段类型 2
    Django 中的用户认证
    Expression<Func<T, bool>>与Func<T, bool>的区别
    根据funID,personID获取最新规划包项目相关信息
    SET ANSI_NULLS ON 在T-SQL中是什么意思
    SQL Server 2008中如何为XML字段建立索引
    PowerDesigner 建模后如何导入到数据库
  • 原文地址:https://www.cnblogs.com/xiaowei-blog/p/3627222.html
Copyright © 2011-2022 走看看