【www.gdgbn.com--php应用】

asp教程.net cookie操作
cookie 限制到某个文件夹或应用程序

httpcookie appcookie = new httpcookie("appcookie");
appcookie.value = "written " + datetime.now.tostring();
appcookie.expires = datetime.now.adddays(1);
appcookie.path = "/application1";
response.cookies.add(appcookie);

cookie清除

httpcookie cookie = request.cookies["username"];
cookie.expires = datetime.now.adddays(-30);
response.cookies.add(cookie);


cookie操作

response.cookies["userinfo"]["username"] = "patrick";
response.cookies["userinfo"]["lastvisit"] = datetime.now.tostring();
response.cookies["userinfo"].expires = datetime.now.adddays(1);

httpcookie acookie = new httpcookie("userinfo");
acookie.values["username"] = "patrick";
acookie.values["lastvisit"] = datetime.now.tostring();
acookie.expires = datetime.now.adddays(1);
response.cookies.add(acookie);

认情况下,一个站点的全部 cookie 都一起存储在客户端上,而且所有 cookie 都会随着对该站点发送的任何请求一起发送到服务器。也就是说,一个站点中的每个页面都能获得该站点的所有 cookie。但是,可以通过两种方式设置 cookie 的范


限制 cookie 的域范围

response.cookies["domain"].value = datetime.now.tostring();
response.cookies["domain"].expires = datetime.now.adddays(1);
response.cookies["domain"].domain = "support.contoso.com";


读取 cookie
浏览器向服务器发出请求时,会随请求一起发送该服务器的 cookie。在 asp.net教程 应用程序中,可以使用 httprequest 对象读取 cookie,该对象可用作 page 类的 request 属性使用。httprequest 对象的结构与 httpresponse 对象的结构基本相同,因此,可以从 httprequest 对象中读取 cookie,并且读取方式与将 cookie 写入 httpresponse 对象的方式基本相同。下面的代码示例演示两种方法,通过这两种方法可获取名为 username 的 cookie 的值,并将其值显示在 label 控件中:

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);
}

本文来源:http://www.gdgbn.com/jiaocheng/29220/