记住我密码不起作用 [英] Remember me password is not working

查看:67
本文介绍了记住我密码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

记住我密码不起作用,请帮助我

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);
            }

        }

推荐答案

确保已打开cookie,并且您可以跨会话编写和读取简单的测试cookie.问题几乎可以肯定与此有关.

查看您实际上从浏览器发送的请求,以及它收到的响应,并查看cookie的设置是否正确.它们是否具有正确的路径?
Make sure cookies are turned on and that you can write and read back a simple test cookie across sessions. The problem is almost certainly related to that.

Have a look at the request you''re actually sending from the browser, and the response it receives, and see if the cookies are being set correctly. Do they have the correct path?


在代码中导入System.Net命名空间.

将以下代码添加到您的登录按钮的click事件中:


import System.Net namespace in your code.

Add the following code to click event of your login button:


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


这篇关于记住我密码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆