• .NET cookie 使用方法


    创建 C# cookie,两种方法

    Response.Cookies["userName"].Value = "patrick"; Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1); HttpCookie aCookie = new HttpCookie("lastVisit"); aCookie.Value = DateTime.Now.ToString(); aCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(aCookie);


    二、读取代码 ,http://www.cnblogs.com/firstcsharp/archive/2013/06/08/3127209.html【详解】

    if(Request.Cookies["userName"] != null)
        Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
    
    if(Request.Cookies["userName"] != null)
    {
        HttpCookie aCookie = Request.Cookies["userName"];
        Label1.Text = Server.HtmlEncode(aCookie.Value);
    }

    三、写个MVC ACTION  关于删除 增加 cookie 的

       public ActionResult Index()
            {
    
    
                if (Request.Cookies["userName"] != null)
                {
                    HttpCookie f = Request.Cookies["userName"];
    
    
                    int limit = Request.Cookies.Count;    //获取当前网站所有cookie名字
                    for (int i = 0; i < limit; i++)
                    {
                        Response.Write(Server.HtmlEncode(Request.Cookies[i].Name));
                    }
    
    
                    Request.Cookies.Remove("userName"); //删除某个cookie名字
    
                    Response.Write(Request.Cookies["userName"] == null ? "userName被删除了":Request.Cookies["userName"].Value);
    
                }
                else
                {
                    HttpCookie aCookie = new HttpCookie("userName");
                    aCookie.Value = DateTime.Now.ToString();
                    aCookie.Expires = DateTime.Now.AddDays(1);
    
                    Response.Cookies.Add(aCookie);
                    HttpCookie p = new HttpCookie("aaaaa");
                    p.Value = "123";
                    p.Expires = DateTime.Now.AddDays(1);
    
    
                    Response.Cookies.Add(p);
    
                }
    
                return View();
            }

    四、c# 与 javascript 增加的cookie 可以相互使用

      <script>  var date = new Date();
            date.setDate(date.getDate() + 7);
            document.cookie = 'user=' + encodeURIComponent("cpuaaa") + ";path=" + "C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/cpu;expires=" + date;
    
    
            document.cookie = "url=cpu.com";
            document.cookie = "gg=cpugege.com";
    
    //以上为增加多个 cookie
    
            alert(document.cookie);
    
    //获取某个字段的COOKIE方法
    
            function getCookie(name) {
                var cookieName = encodeURIComponent(name) + '=';
                var cookieStart = document.cookie.indexOf(cookieName);//indexOf(找到这个字符串的位置)
                if (cookieStart > -1) {
                    var cookieEnd = document.cookie.indexOf(";", cookieStart);//indexOf(索引到某处,从某位置开始索引(数字))
                    if (cookieEnd == -1) {
    
                        cookieEnd = document.cookie.length;
    
                    }
                }
                return document.cookie.substring(cookieStart + cookieName.length, cookieEnd);
            }
    
            alert(getCookie('aaaaa'));// 调用测试
            alert(getCookie('url'));
    
    
        </script

    五、 移除cookie

      Session["mobile"] = null;
            Session["memberid"] = null; 
            Session["nickname"] = null;
            Session.Abandon();
            Session.Clear();
            if (Request.Cookies["member"] != null)
            {
                HttpCookie myCookie = new HttpCookie("member");
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }

  • 相关阅读:
    Mac 删除并关闭.DS_Store
    python 使用AES加密16位
    python 使用AES加密时,提示模块不存在
    sql大全
    Mac 升级10.15后,安装软件时提示‘文件已损坏’的解决办法
    Django model Datefield和DateTimeField类型
    1013. 将数组分成和相等的三个部分
    python 批量pip安装包
    Ajax发送数据
    kafka实战教程(python操作kafka--理论篇)
  • 原文地址:https://www.cnblogs.com/cpugege/p/3558551.html
Copyright © 2020-2023  润新知