本文介绍了记住我密码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
记住我密码不起作用,请帮助我
Remember me password is not working ,help me
protected void btnlogin_Click(object sender, EventArgs e)
{
if (rememberme.Checked == true)
{
if (Request.Cookies["UserName"] != null)
{
Response.Cookies["USERNAME"].Value = log.Text;
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(30);
Response.Cookies["PASSWORD"].Value = pwd.Text;
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(30);
}
}
else
{
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(-30);
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(-30);
}
}
推荐答案
if (chkRemember.Checked)
{
HttpCookie cookie = new HttpCookie("YourAppLogin");
cookie.Values.Add("username", txtUsername.Text);
cookie.Expires = DateTime.Now.AddDays(15);
Response.Cookies.Add(cookie);
}
在上面的代码中,我们首先检查用户是否已选中记住我"
复选框(如果已选中),我们将创建一个HTTPCookie并为其设置到期日期.
现在,您应该将此代码添加到需要登录的每个页面中:
In the code above first we check if user has checked "Remember Me"
checkbox, if its checked then we create a HTTPCookie and we set an expiry date for it.
Now you should add this code to every page that requires login :
if (Request.Cookies["YourApLogin&"] != null)
{
string username = Request.Cookies["YourAppLogin"].Values["username"]);
}
这段代码检查Cookie并返回用户名.
This code checks the cookie and returns Username.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["USERNAME"] != null)
log.Text = Request.Cookies["USERNAME"].Value;
if (Request.Cookies["PASSWORD"] != null)
pwd.Text = Request.Cookies["PASSWORD"].Value;
pwd.Attributes.Add("value", Request.Cookies["PASSWORD"].Value);
//txtpwd.Attributes.Add("values", Request.Cookies["PASSWORD"].Value);
if (Request.Cookies["USERNAME"] != null && Request.Cookies["PASSWORD"] != null)
rememberme.Checked = true;
}
}
像这样的按钮点击事件
in button click event like this
if (rememberme.Checked == true)
{
Response.Cookies["USERNAME"].Value = log.Text;
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(30);
Response.Cookies["PASSWORD"].Value = pwd.Text;
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(30);
//HttpCookie cookie = new HttpCookie();
}
else
{
Response.Cookies["USERNAME"].Expires = DateTime.Now.AddYears(-30);
Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddYears(-30);
}
但这有一些问题,请帮助我
but it has some problem , help me
这篇关于记住我密码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!