当前位置: 编程技术>.net/c#/asp.net
asp.net读取与写入cookie的小例子
来源: 互联网 发布时间:2014-08-30
本文导语: 代码如下: 代码示例: protected void Page_Load(object sender, EventArgs e) { //打开登录页面时获取客户端cookie值并写入前台控件中 HttpCookie cookie = Request.Cookies["name"]; if (cookie == null) { UserName.Text = ""; ...
代码如下:
代码示例:
protected void Page_Load(object sender, EventArgs e)
{
//打开登录页面时获取客户端cookie值并写入前台控件中
HttpCookie cookie = Request.Cookies["name"];
if (cookie == null)
{
UserName.Text = "";
}
else
{
UserName.Text = cookie.Value;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string username = Request.QueryString["UserName"];
string password = Request.QueryString["PassWord"];
if (登录成功)
{
Response.Write("登陆成功"); //www.
HttpCookie cookie=new HttpCookie("name",username);//获取用户的用户名
cookie.Expires = DateTime.Now.AddDays(10);//设置cookie过期时间为10天后
Response.Cookies.Add(cookie);//将cookie写入客户端
}
else
{
Response.Write("登陆失败");
}
}
{
//打开登录页面时获取客户端cookie值并写入前台控件中
HttpCookie cookie = Request.Cookies["name"];
if (cookie == null)
{
UserName.Text = "";
}
else
{
UserName.Text = cookie.Value;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string username = Request.QueryString["UserName"];
string password = Request.QueryString["PassWord"];
if (登录成功)
{
Response.Write("登陆成功"); //www.
HttpCookie cookie=new HttpCookie("name",username);//获取用户的用户名
cookie.Expires = DateTime.Now.AddDays(10);//设置cookie过期时间为10天后
Response.Cookies.Add(cookie);//将cookie写入客户端
}
else
{
Response.Write("登陆失败");
}
}