我想建个cookie数组.sitecookies
里面包含两个cookie:username,userclass
并且还要设置该sitecookies的有效期等.应该如何做啊.还有读取呢?我搜索不到答案.所以来发贴了.请帮帮我吧.
里面包含两个cookie:username,userclass
并且还要设置该sitecookies的有效期等.应该如何做啊.还有读取呢?我搜索不到答案.所以来发贴了.请帮帮我吧.
解决方案 »
- 关于linkbutton的 CommandArgument的问题
- “刷新”和“回发”肯定有区别!自己做了一个例子
- 再开一个,还是问刚才的进程问题!
- 请问文件能不能直接发送给一个页面来处理
- ________调试环境错误, 谢谢前辈们指点!
- FCKeditor无法弹出超级链接窗口
- ASP.Net网络考试系统(C#+SQL Server)
- 怎么将数据库里面的image字段下载为文件,不是附件的方式,而是直接包村为文件?
- VS2012 下的 ascx 被使用过了,但是找不到被使用的aspx页面
- 安装.NET Framework SDK 1.1 版时出现如下错误,是何原因
- 急!! 页面中获取用户控件中ListBox信息时,提示"未将对象引用设置到对象的实例"
- 为什么图片还显示原来的?
Response.Cookies["username"].value="mike";
Response.Cookies["username"].Expires=DateTime.MaxValue; //方式2:
HttpCookie acookie = new HttpCookie("last");
acookie.Value="a";
acookie..Expires=DateTime.MaxValue;
Response.Cookies.Add(acookie);//多值Cookie的写法//方式1:
Response.Cookies["userinfo1"]["name"].value="mike";
Response.Cookies["userinfo1"]["last"].value="a";
Response.Cookies["userinfo1"].Expires=DateTime.MaxValue; //方式2:
HttpCookie cookie = new HttpCookie("userinfo1");
cookie.Values["name"]="mike";
cookie.Values["last"]="a";
cookie.Expires=DateTime.MaxValue;
Response.Cookies.Add(cookie);
读取Cookie
Internet Explorer 将站点的 Cookie 保存在文件名格式为 <user>@<domain>.txt 的文件中,其中 <user> 是您的帐户名。
注意:在获取Cookie的值之前,应该确保该 Cookie 确实存在。否则,您将得到一个异常
If (Request.Cookies["userName"]!=null)
{
string str = Request.Cookies("userName").Value;
}//多值Cookie的读取
If ( Request.Cookies["userInfo1"]!=null )
{
string name=Request.Cookies["userInfo1"]["name"];
string last=Request.Cookies["userInfo1"]["last"];
}
//读取 Cookie 集合
for(int i = 0 ;i<Request.Cookies.Count ;i++)
{
HttpCookie cookies = Request.Cookies;
Response.Write("name="+cookies.Mame+"<br>");
if (cookies.HasKeys )//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl
= aCookie.Values ;
for(int j=0;j<NameColl.Count;j++)
{
Response.Write("子键名="+ NameColl.AllKey[j] +"<br>");
Response.Write("子键值="+ NameColl[j] +"<br>");
} }
else
{
Response.Write("value="+cookies.Value+"<br>");
}
}
运行此代码时,可看到一个名为“ASP.NET_SessionId”的Cookie,ASP.NET用这个 Cookie 来保存您的会话的唯一标识符。修改 Cookie
修改的方法与创建方法相同
删除 Cookie
将其有效期设置为过去的某个日期。当浏览器检查 Cookie 的有效期时,就会删除这个已过期的 Cookie。
HttpCookie cookie = new HttpCookie("userinfo1");
cookie.Expires=DateTime.Now.AddDays(-30);
Response.Cookies.Add(cookie);
旁观
Request.cookies返回的应该是HttpCcookieCollection呀