zoukankan      html  css  js  c++  java
  • .net中如何使用cookie

    比如建立一个名为aspcn,值为灌水小鱼的cookie

    HttpCookie cookie = new HttpCookie["aspcn"];
    cookie.Value = "灌水小鱼";
    Response.AppendCookie(cookie);

    取出Cookie值也很简单

    HttpCookie cookie = Request.Cookies["aspcn"];
    cookieValue = cookie.Value;

    在一个Cookie中储存多个信息,那也没有问题。比如在名为aspcn的cookie下加多个信息

    HttpCookie cookie = new HttpCookie("aspcn");
    cookie.Values.Add("webmaster","灌水小鱼");
    cookie.Values.Add("writer","beige");
    cookie.Values.Add("LinkColor","blue");
    Response.AppendCookie(cookie);

    取出信息也一样简单

    HttpCookie cookie = Request.Cookies["aspcn"];
    value1 = cookies.Values["webmaster"];
    value2 = cookies.Values["writer"];

    HttpCookie cookie = Request.Cookies[strKey];
    if(null == cookie)
    {
    //cookie不存在
    }
    先看看有没有写入客户端



    cookie=new HttpCookie("www.xx.org");
    cookie.Values.Add("UserType", ddlLoginType.SelectedItem.Value);
    cookie.Values.Add("UserName", tbUserName.Text.Trim());
    cookie.Values.Add("Password", tbPassword.Text.Trim());
    cookie.Values.Add("Access",i.ToString());
    Response.AppendCookie(cookie);
    //检查COOKIE是否已经写入浏览器
    cookie = Request.Cookies["www.xx.org"];
    if(cookie==null||cookie.ToString()=="")
    {
    //用session存储
    LoginUser loginUser = new LoginUser();
    loginUser.UserName = tbUserName.Text.Trim();
    loginUser.Password = tbPassword.Text.Trim();
    loginUser.UserType = ddlLoginType.SelectedItem.Value;
    loginUser.Access = i;
    Session["www.xx.org"] = loginUser;
    }

    我以前的代码,没有问题的




    >>>页面仅仅是刷新了一次

    if you debug, does the code get executed at all? by the way, are you doing any validation?

    >>>然后是删除

    >>>Response.Cookies.Remove("MyCookie");
    >>>Response.Redirect("....") // 转向  

    >>>然后是删除

    HttpCookie cookie = Request.Cookies["myinfo"];
            if (cookie != null)
            {
                cookie.Expires = System.DateTime.Now.AddDays(-1);
            }
            Response.Cookies.Add(cookie);

  • 相关阅读:
    latex如何输入正确的 双引号
    【leetcode】Linked List Cycle
    CodeForces 441 A. Valera and Antique Items
    JS封深入了解
    ArcGIS Engine开发之旅04---ARCGIS接口详细说明
    ArcGIS Engine开发之旅03--ArcGIS Engine中的控件
    ArcGIS Engine开发之旅02--ArcGIS Engine中的类库
    ArcGIS Engine开发之旅01---产品组成、逻辑体系结构
    ArcGIS三大文件格式解析
    1.准备工作及实例
  • 原文地址:https://www.cnblogs.com/shylock/p/1936325.html
Copyright © 2011-2022 走看看