欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net 添加Cookie的4种方法

程序员文章站 2024-03-31 12:24:10
复制代码 代码如下://第一种添加cookie方法        httpcookie mycooki...

复制代码 代码如下:

//第一种添加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);