登录页面
if (Login(this.txtName.Text.Trim().ToString(),this.txtPass.Text.Trim().ToString()))
{
HttpCookie cookie = new HttpCookie("user");
cookie.Values.Add("username",this.txtName.Text);
cookie.Values.Add("userpass",this.txtPass.Text);
if (this.ckLogin.Checked)
{
DateTime dat = DateTime.Now;//定义时间对象
TimeSpan ts = new TimeSpan(30, 0, 0, 0);//cookie有效作用时间,具体查msdn
cookie.Expires = dat.Add(ts);//添加作用时间
//cookie.Expires.AddDays(30);
}
Response.AppendCookie(cookie);
Thread.Sleep(2000);
Response.Redirect("index.aspx");
}
else
{
this.lblError.Text = "用户名或密码有误,请重新输入!";
}
首页,index.aspx,里面有一个注销按钮
首先获取,在页面加载的时候
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["user"];
if (cookie == null)
{
Response.Redirect("~/Admin_Manager/Login.aspx");
}
else
{
string name = cookie.Values["username"];
this.lblName.Text = name;
}
}
当点击注销的时候
protected void lkZhuXiao_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["user"];
//首先获取用户名(也可获取其他,例如密码什么的)
string username=cookie.Values["username"];
//移除用户名
cookie.Values.Remove(username);
//设置过期时间
cookie.Expires = DateTime.Now.AddDays(-1);
//重新添加cookie,现在的cookie就是注销了当前用户
Response.Cookies.Add(cookie);
//Request.Cookies["user"].Expires.AddDays(-1);
Response.Redirect("~/Admin_Manager/zhuxiao.aspx");
}