发布网友 发布时间:2022-05-13 12:48
共4个回答
懂视网 时间:2022-05-19 19:49
在程序开发中,使用Cookie时,很少使用如http://jb51.net/article/33590.htm的写法。习惯写成Cookie集合,什么叫做Cookie集合,即是说一个Cookie,它拥有多个值。下面一系列演示,是怎样创建Cookie集合与使用。
InsusBiz
using System;
using System.Web;
/// <summary>
/// Summary description for InsusBiz
/// </summary>
public class InsusBiz
{
private static HttpResponse Response
{
get
{
return HttpContext.Current.Response;
}
}
private static HttpRequest Request
{
get
{
return HttpContext.Current.Request;
}
}
//定义一个Cookie集合
private static HttpCookie InsusCookie
{
get
{
return Request.Cookies["InsusCookie"] as HttpCookie;
}
set
{
if (Request.Cookies["InsusCookie"] != null)
{
Request.Cookies.Remove("InsusCookie");
}
Response.Cookies.Add(value);
}
}
//New Cookie集合
private static HttpCookie NewInsusCookie
{
get
{
HttpCookie httpCookie = new HttpCookie("InsusCookie");
return httpCookie;
}
}
//Remove Cookie集合
public static void RemoveInsusCookie()
{
if (InsusCookie == null)
Response.Cookies.Remove("InsusCookie");
else
Response.Cookies["InsusCookie"].Expires = DateTime.Now.AddDays(-1);
}
//创建一个Cookie,判断用户登录状态
public static bool LoginOk
{
get
{
return InsusCookie == null ? false : bool.Parse(InsusCookie.Values["LoginOk"]);
}
set
{
HttpCookie httpCookie = InsusCookie == null ? NewInsusCookie : InsusCookie;
httpCookie.Values["LoginOk"] = value.ToString();
InsusCookie = httpCookie;
}
}
//创建登录用户的帐号,整站使用
public static string MemberId
{
get
{
return InsusCookie == null ? string.Empty : InsusCookie.Values["MemberId"];
}
set
{
HttpCookie httpCookie = InsusCookie == null ? NewInsusCookie : InsusCookie;
httpCookie.Values["MemberId"] = value;
InsusCookie = httpCookie;
}
}
//如果还有整站使用的Cookie可以写在此,可以参考LoginOK或MemberId的写法。
}
在应用时,你会看到InsusBiz类别下有LoginOk,MemberId和RemoveInsusCookie等属性:
在程序中怎样使用这些cookie呢?如在登录验证成功之后,你需要把登录状态与登录的ID写入Cookie中
InsusBiz.LoginOk = true;
InsusBiz.MemberId = xxx;
在判断用户是否登录时,可以这个去判断:
代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!InsusBiz.LoginOk)
{
//你没有登录
}
}
如果想在任何位置,想取出登录ID:
代码如下:
string memberId = InsusBiz.MemberId;
最后想说的,你想移除Cooke,就可以使用InsusBiz.RemoveInsusCookie就可以了,因为它会把Cookie的过期时间变更为过去。这个通常应用在用户Sign out的事件上。热心网友 时间:2022-05-19 16:57
1、添加Cookie
protected void Button1_Click(objectsender, EventArgs e)
{
HttpCookiecookies = new HttpCookie("website");
//方法一
cookies.Values.Add("name","Jianjie1");
cookies.Values.Add("sex","1");
//方法二
cookies["name"]= "Jianjie2";
cookies["sex"]= "0";
cookies.Expires = DateTime.Now.AddMinutes(60);
Response.Cookies.Add(cookies);
}
2、修改Cookie
新建一个页面CookieModify.aspx,进行测试
下面写法错误:
protected void Button1_Click(objectsender, EventArgs e)
{
HttpCookiecookies = Request.Cookies["website"];
if(cookies != null)
{
cookies.Values["name"] = this.TextBox1.Text;
cookies.Values.Add("newid", "0001");
}
}
再加上一句Response.AppendCookie(cookies);如下
protected void Button1_Click(objectsender, EventArgs e)
{
HttpCookiecookies = Request.Cookies["website"];
if(cookies != null)
{
cookies.Values["name"] = this.TextBox1.Text;
cookies.Values.Add("newid", "0001");
Response.AppendCookie(cookies);
}
}
再新建一个取值页面CookieGet.aspx,用它展示Cookie值,结果竟然报错
Cookies的值为NULL。
反复测试,发现在当前页面上跳转,Cookie取值正常,再打开一个新窗体页面中取值Cookie为NULL,
检查很久,最后尝试在修改页面CookieModify.aspx中增加过期时间:cookies.Expires =DateTime.Now.AddMinutes(60); 修改成功!
代码如下
protected void Button1_Click(objectsender, EventArgs e)
{
HttpCookiecookies = Request.Cookies["website"];
if(cookies != null)
{
cookies.Values["name"] = this.TextBox1.Text;
cookies.Values.Add("newid", "0001");
cookies.Expires = DateTime.Now.AddMinutes(60);
Response.AppendCookie(cookies);
}
}
自己理解:修改Cookie后Response.AppendCookie(cookies); Cookie的默认过期时间是当前会话有效,所以在当前页面中跳转可以正常取值,重新打开一个新的窗体Cookie过期了为空。所以最好还是给Expires赋予过期时间。
3、删除Cookie
删除Cookie中的一个Value键值
protected void btnDeleteItem_Click(objectsender, EventArgs e)
{
HttpCookiecookie = Request.Cookies["website"];
if(cookie != null)
{
cookie.Values.Remove("name");
cookie.Expires = DateTime.Now.AddMinutes(60);
Response.AppendCookie(cookie);
}
}
删除一个Cookie
protected void btnDeleteCookie_Click(objectsender, EventArgs e)
{
HttpCookiecookie = Request.Cookies["website"];
if(cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(cookie);
}
}
以上代码均经过测试,删除成功!
源自长沙软件公司--小房子
热心网友 时间:2022-05-19 18:15
状态保持的时候用嘛! 简单给你说下: 因为它属于连个对象 request对象和response 对象,request对象是用来取cookie的,而response对象是用来存cookie的值,所以用法是:(存) response.cookies["song"].value=值。 (取 ) string jian = request.cookies["songjian"].value ,因为每一个cookie都属于叫一个cookies的集合当中,所以要用索引器的方式进行存取! 相信你明白了吧!热心网友 时间:2022-05-19 19:50
你发个头过来