Cookie application session

•Application 对象是存储于服务器的全局变量
•Cookie 存储信息于客户端
•Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除

 

 

 

1.将信息写入Cookies 中/

方式一
//HttpCookie hcName = new HttpCookie("UserName", this.txtName.Text);
//HttpCookie hcPwd = new HttpCookie("UserPwd", this.txtPassword.Text);
//hcName.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));
//hcPwd.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));

//this.Response.Cookies.Add(hcName);
//this.Response.Cookies.Add(hcPwd);

 

读取

HttpCookie hcPwd = this.Request.Cookies["UserPwd"];
string s = "";
s = hcPwd.Value;

 

方式二

HttpCookie hc = new HttpCookie("User");
hc.Values.Add("name", this.txtName.Text.Trim());
hc.Values.Add("pwd", this.txtPassword.Text.Trim());
hc.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));
//将cookie对象写入客户端
this.Response.Cookies.Add(hc);

读取

HttpCookie hc = this.Request.Cookies["User"];

string s = "";

if (hc != null)
{
s += "从Cookies中读取的姓名是:" + hc.Values["name"].ToString() + "<br/>读取的密码是:" + hc.Values["pwd"].ToString();
}
else
{
s += "没有Cookie对象!";
}

 

2 删除cookie

 

转载于:https://www.cnblogs.com/ChineseMoonGod/p/3729839.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值