cookie实现登录页面记住我和自动登录功能

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["re"]!=null)
{
this.username.Value = Request.Cookies["re"].Value;
}
if (Request.Cookies["pwd"]!=null)
{
this.password.Value = Request.Cookies["pwd"].Value;
}
}

}

protected void Button1_Click(object sender, EventArgs e)
{
//记住我功能
if (rm.Checked)
{
HttpCookie cookie = new HttpCookie("re");
cookie.Value = this.username.Value;
cookie.Expires = DateTime.Now.AddDays(1);//删除cookie时需要将整段重写将“1”改为“-1”即可其他不变(减到比一小即可)
Response.Cookies.Add(cookie);
zddl.Checked =false;
}
//自动登录

if (zddl.Checked)
{
if (ViewState["ji"] == "1")//登陆成功才生成
{


HttpCookie cookie1 = new HttpCookie("re");
cookie1.Value = this.username.Value;
cookie1.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie1);
HttpCookie pwd = new HttpCookie("pwd");
pwd.Value = this.password.Value;
pwd.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(pwd);
}
//判断两个cookie是否为null
rm.Checked = false;
}

string name1 = username.Value;
string password1 = password.Value;
if( name1=="hh" & password1=="23")
{
Response.Write("已登录!");
ViewState["ji"] ="1";
Response.Redirect("购物页1.aspx?name=hh");
}

else { Response.Write("失败!"); }


}
//遍历读cookie
protected void du()//不知道cookie的个数想将所有的cookie都都读出来
{
if (Request.Cookies.Count>0)
{
for (int i=0;i< Request.Cookies.Count;i++ )
{
Response.Write(Request.Cookies[i].Value);
}
}
}

protected void Button2_Click(object sender, EventArgs e)
{
du();
}
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值