简体   繁体   中英

Remember me in Login with Cookies

I tried remember me with cookies but I can not succeed.it throws null refernce exception. How can I fix it?

in pageload method

   if (!string.IsNullOrEmpty(Request.Cookies["mail"].Value) && !string.IsNullOrEmpty(Request.Cookies["pass"].Value)) {

        TextBox1.Text = Request.Cookies["mail"].Value;

        TextBox2.Text = Request.Cookies["pass"].Value;}
    }

in CheckedChanged method

    if (CheckBox1.Checked == true)
    {
        Response.Cookies["mail"].Value = TextBox1.Text;
        Response.Cookies["pass"].Value = TextBox2.Text;
     }

Change

if (!string.IsNullOrEmpty(Request.Cookies["mail"].Value) && 
    !string.IsNullOrEmpty(Request.Cookies["pass"].Value))

By

if (Request.Cookies["mail"] != null && 
    Request.Cookies["pass"] != null &&
    !string.IsNullOrEmpty(Request.Cookies["pass"].Value)
    !string.IsNullOrEmpty(Request.Cookies["mail"].Value))

If Request.Cookies["mail"] or Request.Cookies["pass"] is null your if will throw the exception you describe.

Another suggestion: if you're using Forms Auhentication you can use built in classes to support your login and related features. Reinvent the wheel in relation to security in not a good strategy.

For instance, I see that you're storing the password in plain text on the cookie. This is a very bad idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM