protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie1 = Request.Cookies["username"]; HttpCookie cookie2 = Request.Cookies["password"]; if (cookie1 != null && cookie2!=null) { Response.Write(cookie1.Value + "<br/>"); Response.Write(cookie2.Value + "<br/>"); } } protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie1 = new HttpCookie("username", Guid.NewGuid().ToString()); cookie1.Expires = DateTime.Now.AddMinutes(1); Response.SetCookie(cookie1); HttpCookie cookie2 = new HttpCookie("password", Guid.NewGuid().ToString()); cookie2.Expires = DateTime.Now.AddMinutes(1); Response.SetCookie(cookie2); }
这样的测试代码,每次点击button,都会显示你最新保存的那个cookie。
高手,要怎么改呢,我是菜鸟,刚接触cookies,就想做一个简单登陆界面,换帐号登陆时,cookies的值就换成新帐号
@Sureboy:
你可以参看这个代码进行修改。
CIM.COMM.MyCookie mck = new CIM.COMM.MyCookie(CIM.COMM.CIMCookieNames.CIM_Care);
if (mck != null)
{
string UserID = mck.GetCookieValue("UserID");
if (UserID != "")
{
//创建用来保存用户的血糖设置 cimxt+用户编号
CIM.COMM.MyCookie mc = new CIM.COMM.MyCookie("cimxy" + UserID, 30);
mc.AddCookieValue("UserBJ", Bj);//标记
mc.AddCookieValue("UserSZ", SZ);//数值
mc.AddCookieValue("UserQY", qy);//启用
mc.SaveCookie();
}
}
动态的生成Cookie,OK