.net 4种添加Cookie方法

 //第一种添加Cookie方法
        HttpCookie myCookie = new HttpCookie("userrole");
        myCookie.Values["a"] = "a";
        myCookie.Values[""] = "b";
        myCookie.Expires.AddDays(1);
        Response.Cookies.Add(myCookie);
        //Response.AppendCookie(mycookie);这个也可以添加
        //第一种获取Cookie方法
        Response.Write(Request.Cookies["userrole"].Values["a"].ToString());

        //第二种添加Cookie方法
        HttpCookie myCookie = new HttpCookie("userrole");
        myCookie.Values["a"] = "a";
        myCookie.Values["b"] = "b";
        myCookie.Expires.AddDays(1);
        Response.Cookies.Add(myCookie);
        //第二种读取Cookie方法
        HttpCookie cookie = Request.Cookies["userrole"];
        Response.Write(cookie.Values["a"].ToString());
        Response.Write(cookie.Values["b"].ToString());

        //第三种添加Cookie方法
        HttpCookie myCookie = new HttpCookie("userrole");
        myCookie.Value = "a";
        Response.Cookies.Add(myCookie);
        //第三种读取Cookie方法
        Response.Write(Request.Cookies["userrole"].Value);

        //第四种添加Cookie方法
        HttpContext.Current.Response.Cookies.Add(new HttpCookie("userrole", "超级管理员"));
        Response.Cookies["userrole"].Value = "超级管理员";
        HttpCookie cookie = Request.Cookies["userrole"];
        Response.Write(cookie.Value);
        //第三种读取Cookie方法
        Response.Write(Request.Cookies["userrole"].Value);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。